Issue
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 [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`
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 [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}
In the generated dictionary the information is not displayed correctly, and some indexes are also missing.What could be the problem?
Solution
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', '[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`']
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 [email protected]
28 B [email protected]
12 15 C [email protected]
11 D [email protected]
15 E [email protected]
11 13 F [email protected]
9 7 G [email protected]
1 30 H [email protected]
4 19 I [email protected]
Answered By - user19077881
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.