words = []
for word in df.head(1000).values:
words.append(word)
单词是[['phone',422],['good',374]]
当我使用
时dict(words)
TypeError:'DataFrame'对象不可调用
type(words)
list
我认为单词是一个列表。那么什么可能导致这个错误?
答案 0 :(得分:0)
IIUC如果需要从列A
和B
生成字典,则需要DataFrame.set_index
+ Series.to_dict
:
df = pd.DataFrame({'A':['phone','good'],
'B':[422,374]})
print (df)
A B
0 phone 422
1 good 374
#test if similar output of my sample df
words = []
for word in df.head(1000).values:
words.append(word)
print (words)
[array(['phone', 422], dtype=object), array(['good', 374], dtype=object)]
print (df.set_index('A')['B'].to_dict())
{'phone': 422, 'good': 374}