我有一个.txt文件,名为“ data.txt”,它是字典列表,例如,类似
{"958464": ["item1", "item3"], "917654": [], "1347584": ["item2", "item5", "item6", "item7"], "914082": [], "946178": ["item1", "item4", "item15"], ...}
如何与上述文件一起形成数据框?
我的想法是与另一个数据库进行交叉信息(关于ID),所以我认为像这样的数据框,
ID ITEMS
0 958464 "item1", "item3"
1 917654
2 1347584 "item2", "item5", "item6", "item7"
3 914082
4 946178 "item1", "item3", "item5"
...
或者也许创建一个像这样的数据框
ID Item1 item2 item3 ...
0 958464 1 0 1
1 917654 0 0 0
2 1347584 0 1 0
3 914082 0 0 0
4 946178 1 0 1
...
我尝试了
data = pd.read_csv('data1.txt')
pd.DataFrame(data.items())
答案 0 :(得分:1)
尝试一下:
k = {"958464": ["item1", "item3"], "917654": [],
"1347584": ["item2", "item5", "item6", "item7"], "914082": [], "946178": ["item1", "item4", "item15"]}
import pandas
df1=pandas.DataFrame.from_dict(k,orient='index')
print(df1)
O / P:
0 1 2 3
958464 item1 item3 None None
917654 None None None None
1347584 item2 item5 item6 item7
914082 None None None None
946178 item1 item4 item15 None