python - Pandas 数据框听写

标签 python pandas dictionary

我有一个数据框:

pd.DataFrame([[1,2,3],[111,222,333]], columns=['A', 'B', 'C'])

     A    B    C
0    1    2    3
1  111  222  333
2   11   22   33

我需要将 A 和 C 中的每一行转换为字典。
我应该能够得到这个:

{'1':'3',
 '111':'333',
 '11':'33'}

到目前为止,我还没有找到如何选择应包含哪些列以及如何不包含标题

最佳答案

来自zip

dict(zip(df.A,df.C))
Out[1073]: {1: 3, 11: 33, 111: 333}

更新

from collections import defaultdict
d = defaultdict(dict)
for _,x in df.iterrows():
    d[x['A']][x['B']] = x['C']


d
Out[74]: defaultdict(dict, {1: {2: 3}, 11: {22: 33}, 111: {222: 333}})

关于python - Pandas 数据框听写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49077008/

相关文章:

Python:从嵌入另一个字典和列表的字典中提取值

python - 在 get_text() 中用 <br> 标签分隔

python - 尝试在 nltk_contrib.timex 中使用 ground 方法时出现错误

python - Keras误解了训练数据的形状

python - Pandas:从SettingWithCopyWarning到loc和iloc

python - 添加一个可以将数值分为低中位数和高位数的列?

c++ - STL map 不会删除

python - Pandas 数据帧 : resample with linear interpolation

python - Pandas:如何使用 json 数组分解数据框

javascript - 如何使用 JSON 数据同时更新多个标记位置?传单 JS