python - How to merge two DataFrame columns and apply pandas.to_datetime to it? -


i''m learning use pandas, use data analysis. data supplied csv file, several columns, of need use 4 (date, time, o, c). i'll create new dataframe, uses index datetime64 number, number creating merging first 2 columns, applying pd.to_datetime on merged string.

my loader code works fine:

st = pd.read_csv("c:/data/stockname.txt", names=["date","time","o","h","l","c","vol"]) 

the challenge converting loaded dataframe new one, right format. below works slow. moreover, makes 1 column new datetime64 format, , doesnt make index.

my code

st_new = pd.concat([pd.to_datetime(st.date + " " + st.time), (st.o + st.c) / 2, st.vol],       axis = 1, ignore_index=true) 

what more pythonic way merge 2 columns, , apply function result? how make new column index of dataframe?

you can everythin in read_csv function:

pd.read_csv('test.csv',             parse_dates={'timestamp': ['date','time']},             index_col='timestamp',             usecols=['date', 'time', 'o', 'c']) 

parse_dates tells read_csv function combine date , time column 1 timestamp column , parse timestamp. (pandas smart enough know how parse date in various formats)

index_col sets timestamp column index.

usecols tells read_csv function select subset of columns.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -