我有一个像这样的数据帧:
Date sdate
0 2012-3-12 [2012, 03, 12]
1 2012-3-25 [2012, 03, 25]
2 2012-4-20 [2012, 04, 20]
3 2012-4-12 [2012, 04, 12]
4 2012-4-26 [2012, 04, 26]
我需要提取年,月和日来分隔这样的列
Date sdate year month day
0 2012-3-12 [2012, 03, 12] 2012 03 12
1 2012-3-25 [2012, 03, 25] 2012 03 25
2 2012-4-20 [2013, 04, 20] 2013 04 20
3 2012-4-12 [2015, 06, 12] 2015 06 12
4 2012-4-26 [2011, 08, 26] 2011 08 26
我可以使用for循环来实现吗?
答案 0 :(得分:2)
将apply
与pd.Series
和rename
列
In [784]: df.sdate.apply(pd.Series).rename(columns={0:'year',1:'month',2:'day'})
Out[784]:
year month day
0 2012 3 12
1 2012 3 25
2 2012 4 20
3 2012 4 12
4 2012 4 26
join
原件df
In [785]: df.join(df.sdate.apply(pd.Series).rename(columns={0:'year',1:'month',2:'day'}))
Out[785]:
Date sdate year month day
0 2012-3-12 [2012, 3, 12] 2012 3 12
1 2012-3-25 [2012, 3, 25] 2012 3 25
2 2012-4-20 [2012, 4, 20] 2012 4 20
3 2012-4-12 [2012, 4, 12] 2012 4 12
4 2012-4-26 [2012, 4, 26] 2012 4 26
或者,将列名称提供为index
In [786]: df.sdate.apply(lambda x: pd.Series(x, index=['year', 'month', 'day']))
Out[786]:
year month day
0 2012 3 12
1 2012 3 25
2 2012 4 20
3 2012 4 12
4 2012 4 26