pandas iterrows()跳过行

8ehkhllq  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(93)

我正在使用下面的代码来覆盖我的框架数据
输入:

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}


在生成的字典中,信息没有正确显示,并且一些索引也丢失了。可能是什么问题?

utugiqy6

utugiqy61#

目前还不清楚你希望字典如何形成和显示,但这可能更接近你想要的:

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`']


如果你想看到所有的数据包,包括重复的数据包,那么不要使用使用mes dia作为关键字的字典,因为重复的关键字是不允许的。例如,你可以用途:

df1 = dateframe1.set_index(['mes', 'dia'])

print(df1)


其给出:

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]

相关问题