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]:
 
  c1 c2
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]:
 
  a b c
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]:
 
  a b c
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]:
 
  exam1 exam2
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]:
 
  exam1 exam2
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]:
 
  a b c
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM