所以我得到的字典如下:
{'a': [[time1, a1], [time2, a2],[time100,a100]], 'b': [[time1, b1], [time2, b2],[time100,b100]], 'c': [[time1, c1], [time2, c2],[time100,c100]]}
每个列表的第一项是 Unix 时间戳中的时间,我希望时间是每一行和列 = ['a','b','c']。每个列表中的第二个项目也位于每个受尊重的单元格中。预期成绩:
a b c
time1. a1. b1. c1
time2. a2. b2 c2
time100
本质上,每个列表中的时间都是相同的,无论键如何。我想选择超时并将每个列表中的第二个项目放到他们每个受尊重的列中。代码是怎样的?
d={'a': [['time1', 'a1'], ['time2', 'a2'],['time100','a100']], 'b': [['time1', 'b1'], ['time2', 'b2'],['time100','b100']], 'c': [['time1', 'c1'], ['time2', 'c2'],['time100','c100']]}
尝试通过DataFrame()
和apply()
和reset_index()
:
#import pandas as pd
df=pd.DataFrame(d).apply(pd.Series.explode).reset_index(drop=True)
#you can also use agg() in place of apply()
现在我们将过滤出结果:
c=df.index%2==0
df=df[~c].set_index(df.loc[c,'a'].values)
#OR
df=df[~c].set_index(df[c]['a'].values)
的输出df
:
a b c
time1 a1 b1 c1
time2 a2 b2 c2
time100 a100 b100 c100
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句