我使用下面的代码来迭代我的数据框数据
输入:
dataframe1 = pandas.read_csv("bday.csv")
print (dataframe1)
输出:
nombre email mes dia
0 A [email protected] 11 29
1 B [email protected] 11 28
2 C [email protected] 12 15
3 D [email protected] 12 11
4 E [email protected] 12 15
5 F [email protected] 11 13
6 G [email protected] 9 7
7 H [email protected] 1 30
8 I [email protected] 4 19`
然后我使用 python 理解来生成字典
输入:
dict_bday = {(row["mes"],row["dia"]):row for index,row in dataframe1.iterrows()}
print(dict_bday)
输出:
{(11, 29): nombre A
email [email protected]
mes 11
dia 29
Name: 0, dtype: object, (11, 28): nombre B
email [email protected]
mes 11
dia 28
Name: 1, dtype: object, (12, 15): nombre E
email [email protected]
mes 12
dia 15
Name: 4, dtype: object, (12, 11): nombre D
email [email protected]
mes 12
dia 11
Name: 3, dtype: object, (11, 13): nombre F
email [email protected]
mes 11
dia 13
Name: 5, dtype: object, (9, 7): nombre G
email [email protected]
mes 9
dia 7
Name: 6, dtype: object, (1, 30): nombre H
email [email protected]
mes 1
dia 30
Name: 7, dtype: object, (4, 19): nombre I
email [email protected]
mes 4
dia 19
Name: 8, dtype: object}
生成的词典中信息显示不正确,并且部分索引也缺失。可能是什么问题?
尚不清楚您期望如何形成和显示词典,但这可能更接近您想要的:
dict_bday = {(row["mes"],row["dia"]): list(row.values) for index,row in dataframe1.iterrows()}
for entry in dict_bday.items():
print(entry[0], entry[1])
给予
(11, '29') ['A', '[email protected]', 11, '29']
(11, '28') ['B', '[email protected]', 11, '28']
(12, '15') ['E', '[email protected]', 12, '15']
(12, '11') ['D', '[email protected]', 12, '11']
(11, '13') ['F', '[email protected]', 11, '13']
(9, '7') ['G', '[email protected]', 9, '7']
(1, '30') ['H', '[email protected]', 1, '30']
(4, '19`') ['I', '[email protected]', 4, '19`']