将列值重整为行,并在末尾加上“标识符”列

时间:2019-03-28 13:01:15

标签: pandas pandas-groupby

我有与不同传感器(即A1_Pin,A2_Pin等)相关的功率测量值。这些测量值以列的形式记录在文件中。数据带有时间戳唯一记录。

df1 = pd.DataFrame({'DateTime': ['12/12/2019', '12/13/2019', '12/14/2019', 
'12/15/2019', '12/16/2019'],
'A1_Pin': [2, 8, 8, 3, 9],
'A2_Pin': [1, 2, 3, 4, 5],
'A3_Pin': [85, 36, 78, 32, 75]})

我想重新整理表格,使每一行对应一个传感器。最后一列表示行数据所属的传感器ID。

决赛桌应该像:

df2 = pd.DataFrame({'DateTime': ['12/12/2019', '12/12/2019', '12/12/2019', 
'12/13/2019', '12/13/2019','12/13/2019', '12/14/2019', '12/14/2019', 
'12/14/2019', '12/15/2019','12/15/2019', '12/15/2019', '12/16/2019', 
'12/16/2019', '12/16/2019'],
'Power': [2, 1, 85,8, 2, 36, 8,3,78, 3, 4, 32, 9, 5, 75],
'ModID': ['A1_PiN','A2_PiN','A3_PiN','A1_PiN','A2_PiN','A3_PiN',                               
'A1_PiN','A2_PiN','A3_PiN','A1_PiN','A2_PiN','A3_PiN',
'A1_PiN','A2_PiN','A3_PiN']})

我尝试了Groupby,Melt,Reshape,Stack和Loops,但无法做到。如果有人可以帮忙?谢谢

2 个答案:

答案 0 :(得分:1)

我会尝试这样的事情:

df1.set_index('DateTime').unstack().reset_index()

答案 1 :(得分:1)

尝试stack时,您处在良好的轨道上。您需要先set_index,然后再reset_index,例如:

df2 = df1.set_index('DateTime').stack().reset_index(name='Power')\
         .rename(columns={'level_1':'ModID'}) #to fit the names your expected output

您会得到:

print (df2)
      DateTime   ModID  Power
0   12/12/2019  A1_Pin      2
1   12/12/2019  A2_Pin      1
2   12/12/2019  A3_Pin     85
3   12/13/2019  A1_Pin      8
4   12/13/2019  A2_Pin      2
5   12/13/2019  A3_Pin     36
6   12/14/2019  A1_Pin      8
7   12/14/2019  A2_Pin      3
8   12/14/2019  A3_Pin     78
9   12/15/2019  A1_Pin      3
10  12/15/2019  A2_Pin      4
11  12/15/2019  A3_Pin     32
12  12/16/2019  A1_Pin      9
13  12/16/2019  A2_Pin      5
14  12/16/2019  A3_Pin     75