遍历df后,统计每行的key 与value ,合并至字典,输出字典结果
- 读取csv or Excel文件
import pandas as pd
country_users = dict()
df = pd.read_excel(r"bbbbbb.xlsx",usecols=[2,5])
#打印出所需的列,用iloc
print(df.iloc[:,0:2])
结果
- df.groupby打包某一列作为字典的key,另一列作为value
country_users = df.groupby('整理国家名')['times'].apply(list).to_dict()
结果
{False: [149, 40, 38, 38, 31, 30, 30, 29, 26, 25, 24, 23, ],
' Afghanistan': [2],
' Albania': [6],
' Andorra': [7],
' Argentina': [70, 69, 34, 3],
' Austria': [68, 22, 2],
' Belarus': [3],
' Belgium': [62, 19, 3],
' Botswana': [6],
' Brazil': [96, 90, 59, 52, 37, 21, 19, 7, 5, 2, 2],
" C?te d'Ivoire": [3]}