简体   繁体   English

如何在同一数据帧(Python,Pandas)中合并1列中的2列?

[英]How to merge 2 columns in 1 within same dataframe (Python, Pandas)?

I'm following tutorial of Wes McKinney on using pandas/python for trading backtesting ( http://youtu.be/6h0IVlp_1l8 ). 我正在关注使用pandas / python进行回溯测试的Wes McKinney教程( http://youtu.be/6h0IVlp_1l8 )。
After pd.read_csv(...) he's using 'dt' (datetime) column as index of dataframe. 在pd.read_csv(...)之后,他使用'dt'(日期时间)列作为数据帧的索引。

df.index = pd.to_datetime(df.pop('dt'))

However, my data has 2 separate columns, 'Date[G]' and 'Time[G]' and the data inside is something like 04-JAN-2013,00:00:00.000 (comma-separated). 但是,我的数据有2个单独的列,'Date [G]'和'Time [G]',里面的数据类似于04-JAN-2013,00:00:00.000(以逗号分隔)。

How do i modify that line of code in order to do the same? 我如何修改这行代码才能做同样的事情? Ie merge two columns within one data frame, and then delete it. 即在一个数据框内合并两列,然后将其删除。 Or is there a way to do that during read_csv itself? 或者有没有办法在read_csv本身期间这样做?

Thanks for all answers. 谢谢你的所有答案。

You should be able to concat two columns using apply() and then use to_datetime(). 您应该能够使用apply()连接两列,然后使用to_datetime()。 To remove columns from dataframe use drop() or just select columns you need: 要从数据框中删除列,请使用drop()或只选择所需的列:

df['dt'] = pd.to_datetime(df.apply(lambda x: x['Date[G]'] + ' ' + x['Time[G]'], 1))


df = df.drop(['Date[G]', 'Time[G]'], 1)
# ..or
# df = df[['dt', ...]]

df.set_index('dt', inplace = True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM