简体   繁体   English

Pandas:根据 DataFrame 中的其他列在 DataFrame 中创建新列

[英]Pandas: Create new column in DataFrame based on other column in DataFrame

I have a pandas DataFrame called df that has the following data:我有一个名为 df 的 Pandas DataFrame,它具有以下数据:

Index    SourceDate
0        AUG_2013
1        SEP_2013
2        JAN_2012

I need to add an additional column that turns each of these dates into the following ConvertedDate Column.我需要添加一个额外的列,将这些日期中的每一个转换为以下 ConvertedDate 列。 This column will have the date in YYYY-MM-DD format with the day always being 01.此列的日期格式为 YYYY-MM-DD,日期始终为 01。

Index    SourceDate    ConvertedDate
0        AUG_2013      2013-08-01
1        SEP_2013      2013-09-01
2        JAN_2012      2012-01-01

I attempted doing this with:我尝试这样做:

df['ConvertedDate'] = time.strptime(str.replace(str.rsplit(df.SourceDate,'_',1)[0],'_','-01-'),'%b-%d-%Y')

Unfortunately this does not work since df.SourceDate is a Series, and string functions won't work on a Series.不幸的是,这不起作用,因为 df.SourceDate 是一个系列,并且字符串函数不适用于系列。

Use to_datetime and pass a format string:使用to_datetime并传递格式字符串:

In [64]:
df['ConvertedDate'] =pd.to_datetime(df['SourceDate'], format='%b_%Y')
df

Out[64]:
   Index SourceDate ConvertedDate
0      0   AUG_2013    2013-08-01
1      1   SEP_2013    2013-09-01
2      2   JAN_2012    2012-01-01

The python datetime format string specifiers can be found here可以在此处找到 python 日期时间格式字符串说明符

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

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