我有以下数据框:
Date/Time P1 P2 P83 ON OFF
2014-01-13 11:41 path 83
2014-01-13 11:41 offline
2014-03-26 14:37 path 1
2014-03-26 14:37 offline
2014-03-26 14:37 path 83
2014-03-26 15:33 path 1
2014-03-26 15:33 online
2014-04-25 5:44 path 2
2014-04-25 5:44 offline
我想合并具有相同索引的行并填写列。永远不会出现一列具有相同索引值的情况,即列值不会崩溃。所需的输出是这样的数据帧:
Date/Time P1 P2 P3 ON OFF
2014-01-13 11:41 path 83 offline
2014-03-26 14:37 path 1 path 83 offline
2014-03-26 15:33 path 1 online
2014-04-25 5:44 path 2 offline
非常感谢:)
答案 0 :(得分:1)
按密钥分组并应用.first()
agg = df.groupby('Date/Time').first()
为“状态”创建新列:
agg['STATUS'] = agg['ON'].fillna(agg['OFF'])
根据需要删除列:
agg.drop(['ON', 'OFF'], axis=1, inplace=True)
让您agg
为:
P1 P2 P83 STATUS
Date/Time
2014-01-13 11:41 NaN NaN path 83 offline
2014-03-26 14:37 path 1 NaN path 83 offline
2014-03-26 15:33 path 1 NaN NaN online
2014-04-25 5:44 NaN path 2 NaN offline