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]:
与此相关的有如下:
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"])
In [14]:
for name, col in df.iteritems():
print(name)
print(col)
In [18]:
for i in df.itertuples():
print(i)
print(getattr(i, 'c1'), getattr(i, 'c2'))
In [19]:
for i in df.itertuples(index=True, name='Pandas'):
print(i)
print(getattr(i, 'c1'), getattr(i, 'c2'))
In [20]:
for i in df.itertuples(index=False, name='nihao'):
print(i)
print(getattr(i, 'c1'), getattr(i, 'c2'))
第二种方案: applymap() 函数可以对DataFrame里的每个值进行处理,然后返回一个新的DataFrame
In [22]:
df = pd.DataFrame({
'a': [1, 2, 3],
'b': [10, 20, 30],
'c': [5, 10, 15]
})
df
Out[22]:
In [23]:
def add_one(x):
return x + 1
df.applymap(add_one)
Out[23]:
一个栗子:
这里有一组数据是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]:
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]:
第三种方案:iloc
In [26]:
df = pd.DataFrame({
'a': [1, 2, 3],
'b': [10, 20, 30],
'c': [5, 10, 15]
})
df
Out[26]:
In [28]:
for i in range(0, len(df)):
print(df.iloc[i]['a'], df.iloc[i]['b'],df.iloc[i]['c'])