![](/img/trans.png)
[英]Convert JSON File which contains multiple dictionary to 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.