如何在同一数据框(Python,Pandas)中合并1中的2列? [英] How to merge 2 columns in 1 within same dataframe (Python, Pandas)?

查看:81
本文介绍了如何在同一数据框(Python,Pandas)中合并1中的2列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Wes McKinney的教程,该教程使用pandas/python进行交易回测( http://youtu.be/6h0IVlp_1l8 ).
在pd.read_csv(...)之后,他使用"dt"(日期时间)列作为数据帧的索引.

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

但是,我的数据有2个单独的列,"Date [G]"和"Time [G]",内部数据类似04-JAN-2013,00:00:00.000(以逗号分隔). /p>

我如何修改该行代码以执行相同操作? IE.在一个数据框中合并两列,然后将其删除.还是有办法在read_csv本身中做到这一点?

感谢所有答案.

解决方案

您应该能够使用apply()然后使用to_datetime()合并两列. 要从数据帧中删除列,请使用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)

I'm following tutorial of Wes McKinney on using pandas/python for trading backtesting (http://youtu.be/6h0IVlp_1l8).
After pd.read_csv(...) he's using 'dt' (datetime) column as index of dataframe.

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).

How do i modify that line of code in order to do the same? I.e. merge two columns within one data frame, and then delete it. Or is there a way to do that during read_csv itself?

Thanks for all answers.

解决方案

You should be able to concat two columns using apply() and then use to_datetime(). To remove columns from dataframe use drop() or just select columns you need:

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)

这篇关于如何在同一数据框(Python,Pandas)中合并1中的2列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆