繁体   English   中英

需要将包含两个列表的字典转换为 Pandas DataFrame

[英]Need to convert a dictionary which contains two lists into Pandas DataFrame

所以我得到的字典如下:

{'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)

output 的df

            a       b       c
time1       a1      b1      c1
time2       a2      b2      c2
time100     a100    b100    c100

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM