需要将包含两个列表的字典转换为 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
基本上每个列表中的时间都是相同的,无论 key.I 想要选择时间并将每个列表中的第二个项目放在他们各自尊重的列中。
代码是什么样子的?
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
所以我得到的字典如下:
{'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
基本上每个列表中的时间都是相同的,无论 key.I 想要选择时间并将每个列表中的第二个项目放在他们各自尊重的列中。 代码是什么样子的?
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