我有一个 1000 x 6 维数据框,其中一列的标题是“日期”,其中日期以“JAN2014”、“JUN2002”等格式显示...我想将此列拆分为两个单独的列:“Year”和“Month”所以JAN将在“Month”列中,2014将在“Year”列中等等。谁能告诉我如何在Python中做到这一点?
You can use the str accessor and indexing:
df['Month'] = df['Date'].str[:3]
df['Year'] = df['Date'].str[3:]
Example:
df = pd.DataFrame({'Date':['JAN2014','JUN2002']})
df['Month'] = df['Date'].str[:3]
df['Year'] = df['Date'].str[3:]
print(df)
Output:
Date Month Year
0 JAN2014 JAN 2014
1 JUN2002 JUN 2002
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.