示例输入数据集是:
0 2017-11-17 10:23:28.691 788 756 789 780
2017-11-17 10:23:29.731 788 783 0 0
2017-11-17 10:23:30.655 747 0 0 0
2017-11-17 10:23:31.627 766 0 0 0
2017-11-17 10:23:32.606 807 0 0 0
示例输出数据集我想要的内容:
0 2017-11-17 10:23:28.691 788
0 2017-11-17 10:23:28.691 756
0 2017-11-17 10:23:28.691 789
0 2017-11-17 10:23:28.691 780
2017-11-17 10:23:29.731 788
2017-11-17 10:23:29.731 783
2 2017-11-17 10:23:30.655 747
2017-11-17 10:23:31.627 766
4 2017-11-17 10:23:32.606 807
我怎么能用python或pandas做到这一点?或者还有其他技术吗?
答案 0 :(得分:1)
使用set_index
+ stack
,然后过滤掉0
行,最后从DataFrame
创建Series
:
print (df)
date a b c d
0 2017-11-17 10:23:28.691 788 756 789 780
1 2017-11-17 10:23:29.731 788 783 0 0
2 2017-11-17 10:23:30.655 747 0 0 0
3 2017-11-17 10:23:31.627 766 0 0 0
4 2017-11-17 10:23:32.606 807 0 0 0
a = df.set_index('date').stack()
df = a[a != 0].reset_index(drop=True, level=1).reset_index(name='a')
print (df)
date a
0 2017-11-17 10:23:28.691 788
1 2017-11-17 10:23:28.691 756
2 2017-11-17 10:23:28.691 789
3 2017-11-17 10:23:28.691 780
4 2017-11-17 10:23:29.731 788
5 2017-11-17 10:23:29.731 783
6 2017-11-17 10:23:30.655 747
7 2017-11-17 10:23:31.627 766
8 2017-11-17 10:23:32.606 807
答案 1 :(得分:0)
replace
+ stack
df.set_index('A').replace({0:np.nan}).stack().reset_index(name='Value').drop('level_1',1)
Out[22]:
A Value
0 2017-11-1710:23:28.691 788.0
1 2017-11-1710:23:28.691 756.0
2 2017-11-1710:23:28.691 789.0
3 2017-11-1710:23:28.691 780.0
4 2017-11-1710:23:29.731 788.0
5 2017-11-1710:23:29.731 783.0
6 2017-11-1710:23:30.655 747.0
7 2017-11-1710:23:31.627 766.0
8 2017-11-1710:23:32.606 807.0
答案 2 :(得分:0)
你可以使用融化
df.replace({0: np.nan}).melt(id_vars='datetime', value_vars=['val1', 'val2', 'val3', 'val4']).dropna().drop('variable', axis = 1)
datetime value
0 0 2017-11-17 10:23:28.691 788.0
1 1 2017-11-17 10:23:29.731 788.0
2 2 2017-11-17 10:23:30.655 747.0
3 3 2017-11-17 10:23:31.627 766.0
4 4 2017-11-17 10:23:32.606 807.0
5 0 2017-11-17 10:23:28.691 756.0
6 1 2017-11-17 10:23:29.731 783.0
10 0 2017-11-17 10:23:28.691 789.0
15 0 2017-11-17 10:23:28.691 780.0