将日期转换为标准化日期列

时间:2018-01-18 15:59:06

标签: python pandas

对于以下数据框:

df = pd.DataFrame({'Name': {0: "A", 1: "A", 2:"B", 3: "B", 4: "A"}, 
       'Spec1': {0: '1', 1: '1', 2:'5',
                3: '5', 4: '1'},
       'Spec2': {0: '2a', 1: '2a', 2:np.nan,
                3: np.nan, 4: '2a'},
       'Date': {0: '10/1/2007', 1: '11/1/2007', 2:'7/1/2009',
                3: '8/1/2009', 4: '12/1/2007'},
       'Value': {0: 5, 1: 10, 2: 3,
                3: 4, 4: 8}            
              }, columns=['Name', 'Spec1', 'Spec2','Date','Value'])
df['Date']=pd.to_datetime(df['Date'])


    Name    Spec1   Spec2   Date    Value
0       A       1       2a  2007-10-01  5
1       A       1       2a  2007-11-01  10
2       B       5       NaN 2009-07-01  3
3       B       5       NaN 2009-08-01  4
4       A       1       2a  2007-12-01  8

我想将数据帧转换为:

    Name    Spec1   Spec2   Date1   Date2 Date3
0   A       1       2a      5       10    8
1   B       5       NaN     3       4     NaN

我认为我应该能够调整它但不确定如何处理将日期从实际日期转移到“第一个日期实例,第二个日期实例”列。

1 个答案:

答案 0 :(得分:4)

你只需要cumcount在这里建立一个新的密钥

df['key']=df.groupby('Name').cumcount()+1

df.set_index(['Name','Spec1','Spec2','key']).Value.unstack().add_prefix('Date').reset_index()
Out[11]: 
key Name  Spec1 Spec2  Date1  Date2  Date3
0      A      1    2a    5.0   10.0    8.0
1      B      5   NaN    3.0    4.0    NaN