I am using this code below to iterate over my dataframe data
Input:
dataframe1 = pandas.read_csv("bday.csv")
print (dataframe1)
Output:
nombre email mes dia
0 A adasd1@gmail.com 11 29
1 B adasd2@gmail.com 11 28
2 C adasd3@gmail.com 12 15
3 D adasd4@gmail.com 12 11
4 E adasd5@gmail.com 12 15
5 F adasd6@gmail.com 11 13
6 G adasd7@gmail.com 9 7
7 H adasd8@gmail.com 1 30
8 I adasd9@gmail.com 4 19`
then i use python comprehension for generate a dictionary
Input:
dict_bday = {(row["mes"],row["dia"]):row for index,row in dataframe1.iterrows()}
print(dict_bday)
Output:
{(11, 29): nombre A
email adasd1@gmail.com
mes 11
dia 29
Name: 0, dtype: object, (11, 28): nombre B
email adasd2@gmail.com
mes 11
dia 28
Name: 1, dtype: object, (12, 15): nombre E
email adasd5@gmail.com
mes 12
dia 15
Name: 4, dtype: object, (12, 11): nombre D
email adasd4@gmail.com
mes 12
dia 11
Name: 3, dtype: object, (11, 13): nombre F
email adasd6@gmail.com
mes 11
dia 13
Name: 5, dtype: object, (9, 7): nombre G
email adasd7@gmail.com
mes 9
dia 7
Name: 6, dtype: object, (1, 30): nombre H
email adasd8@gmail.com
mes 1
dia 30
Name: 7, dtype: object, (4, 19): nombre I
email adasd9@gmail.com
mes 4
dia 19
Name: 8, dtype: object}
In the generated dictionary the information is not displayed correctly, and some indexes are also missing.What could be the problem?
It is not clear how you expect the Dictionary to be formed and displayed, but this might be nearer what you want:
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])
gives
(11, '29') ['A', 'adasd1@gmail.com', 11, '29']
(11, '28') ['B', 'adasd2@gmail.com', 11, '28']
(12, '15') ['E', 'adasd5@gmail.com', 12, '15']
(12, '11') ['D', 'adasd4@gmail.com', 12, '11']
(11, '13') ['F', 'adasd6@gmail.com', 11, '13']
(9, '7') ['G', 'adasd7@gmail.com', 9, '7']
(1, '30') ['H', 'adasd8@gmail.com', 1, '30']
(4, '19`') ['I', 'adasd9@gmail.com', 4, '19`']
If you want to see all Entries including duplicates then don't use a Dictionary using mes dia
as keys because repeated keys are not allowed. For example you could use:
df1 = dateframe1.set_index(['mes', 'dia'])
print(df1)
which gives:
nombre email
mes dia
11 29 A adasd1@gmail.com
28 B adasd2@gmail.com
12 15 C adasd3@gmail.com
11 D adasd4@gmail.com
15 E adasd5@gmail.com
11 13 F adasd6@gmail.com
9 7 G adasd7@gmail.com
1 30 H adasd8@gmail.com
4 19 I adasd9@gmail.com