如何在同一数据帧(Python、Pandas)中将 2 列合并为 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19594562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:16:51  来源:igfitidea点击:

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

pythonpandas

提问by danilam

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.

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

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? 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?

我如何修改该行代码以执行相同的操作?即合并一个数据框中的两列,然后将其删除。或者有没有办法在 read_csv 本身期间做到这一点?

Thanks for all answers.

感谢所有的答案。

回答by lowtech

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:

您应该能够使用 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)