我想用DataFrame读取dict,但结果只读取了没有读取值的列。为什么会这样?
In [65]: i
Out[65]:
{'Alias': 'S510994824',
'AppAccountFlag': 0,
'AttrStatus': 234337,
'ChatRoomId': 0,
'City': '武汉',
......
......
......
'VerifyFlag': 0}
In [66]: DataFrame(i,columns=i.keys())
Out[66]:
Empty DataFrame
Columns: [StarFriend, PYQuanPin, SnsFlag, HeadImgUrl, RemarkPYQuanPin, UniFriend, ContactFlag, RemarkName, AttrStatus, City, Uin, Alias, PYInitial, RemarkPYInitial, Statues, MemberCount, HideInputBarFlag, AppAccountFlag, EncryChatRoomId, VerifyFlag, Sex, Signature, DisplayName, MemberList, Province, OwnerUin, NickName, UserName, ChatRoomId, KeyWord]
Index: []
[0 rows x 30 columns]
为什么看不懂这些值?
答案 0 :(得分:2)
对我来说,作品传递index
参数:
import pandas as pd
d = {'Alias': 'S510994824',
'AppAccountFlag': 0,
'AttrStatus': 234337,
'ChatRoomId': 0,
'City': '武汉',
'VerifyFlag': 0}
df = pd.DataFrame(d, index=[0])
print (df)
Alias AppAccountFlag AttrStatus ChatRoomId City VerifyFlag
0 S510994824 0 234337 0 武汉 0
另一个解决方案是添加[]
:
df = pd.DataFrame([d])
print (df)
Alias AppAccountFlag AttrStatus ChatRoomId City VerifyFlag
0 S510994824 0 234337 0 武汉 0
最后一个解决方案DataFrame.from_dict
:
df = pd.DataFrame.from_dict(d, orient='index')
#set column name
df.columns = ['col']
print (df)
col
VerifyFlag 0
Alias S510994824
AttrStatus 234337
ChatRoomId 0
City 武汉
AppAccountFlag 0