04如何遍历pandas当中dataframe的元素

04如何遍历pandas当中dataframe的元素

In [1]:
import pandas as pd
In [2]:
inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
df = pd.DataFrame(inp)
df
Out[2]:
 
 c1c2
0 10 100
1 11 110
2 12 120
 

与此相关的有如下:

iterrows() : 将DataFrame迭代成(index ,series)

iteritems(): 将DataFrame迭代成(列名,series)

itertuples(): 将DataFrame迭代成元组

方法一:df.iterrows()

In [11]:
for index, row in df.iterrows():
    print(index, row["c1"], row["c2"])
 
0 10 100
1 11 110
2 12 120
In [14]:
for name, col in df.iteritems():
    print(name)
    print(col)
 
c1
0    10
1    11
2    12
Name: c1, dtype: int64
c2
0    100
1    110
2    120
Name: c2, dtype: int64
In [18]:
for i in df.itertuples():
    print(i)
    print(getattr(i, 'c1'), getattr(i, 'c2'))
 
Pandas(Index=0, c1=10, c2=100)
10 100
Pandas(Index=1, c1=11, c2=110)
11 110
Pandas(Index=2, c1=12, c2=120)
12 120
In [19]:
for i in df.itertuples(index=True, name='Pandas'):
    print(i)
    print(getattr(i, 'c1'), getattr(i, 'c2'))
 
Pandas(Index=0, c1=10, c2=100)
10 100
Pandas(Index=1, c1=11, c2=110)
11 110
Pandas(Index=2, c1=12, c2=120)
12 120
In [20]:
for i in df.itertuples(index=False, name='nihao'):
    print(i)
    print(getattr(i, 'c1'), getattr(i, 'c2'))
 
nihao(c1=10, c2=100)
10 100
nihao(c1=11, c2=110)
11 110
nihao(c1=12, c2=120)
12 120
 

第二种方案: applymap() 函数可以对DataFrame里的每个值进行处理,然后返回一个新的DataFrame

In [22]:
df = pd.DataFrame({
    'a': [1, 2, 3],
    'b': [10, 20, 30],
    'c': [5, 10, 15]
})
df
Out[22]:
 
 abc
0 1 10 5
1 2 20 10
2 3 30 15
In [23]:
def add_one(x):
    return x + 1
df.applymap(add_one)
Out[23]:
 
 abc
0 2 11 6
1 3 21 11
2 4 31 16
 

一个栗子:

这里有一组数据是10个学生的两次考试成绩,要求把成绩转换成ABCD等级:</br>

转换规则是:</br>

90-100 -> A</br> 80-89 -> B</br> 70-79 -> C</br> 60-69 -> D</br> 0-59 -> F</br>

In [24]:
grades_df = pd.DataFrame(
    data={'exam1': [43, 81, 78, 75, 89, 70, 91, 65, 98, 87],
          'exam2': [24, 63, 56, 56, 67, 51, 79, 46, 72, 60]},
    index=['Andre', 'Barry', 'Chris', 'Dan', 'Emilio',
           'Fred', 'Greta', 'Humbert', 'Ivan', 'James']
)
grades_df
Out[24]:
 
 exam1exam2
Andre 43 24
Barry 81 63
Chris 78 56
Dan 75 56
Emilio 89 67
Fred 70 51
Greta 91 79
Humbert 65 46
Ivan 98 72
James 87 60
In [25]:
def convert_to_letter(score):
    if (score >= 90):
        return 'A'
    elif (score >= 80):
        return 'B'
    elif (score >= 70):
        return 'C'
    elif (score >= 60):
        return 'D'
    else:
        return 'F'
grades_df.applymap(convert_to_letter)
Out[25]:
 
 exam1exam2
Andre F F
Barry B D
Chris C F
Dan C F
Emilio B D
Fred C F
Greta A C
Humbert D F
Ivan A C
James B D
 

第三种方案:iloc

In [26]:
df = pd.DataFrame({
    'a': [1, 2, 3],
    'b': [10, 20, 30],
    'c': [5, 10, 15]
})
df
Out[26]:
 
 abc
0 1 10 5
1 2 20 10
2 3 30 15
In [28]:
for i in range(0, len(df)):
    print(df.iloc[i]['a'], df.iloc[i]['b'],df.iloc[i]['c'])
 
1 10 5
2 20 10
3 30 15
原文地址:https://www.cnblogs.com/xinmomoyan/p/13706188.html