1. 程式人生 > >pandas怎樣對資料進行遍歷

pandas怎樣對資料進行遍歷

pandas.DataFrame.iterrows

DataFrame.iterrows()
迭代(iterate)覆蓋整個DataFrame的行中,返回(index, Series)對。

import numpy as np
import pandas as pd

def _map(data, exp):                  
    for index, row in data.iterrows():   # 獲取每行的index、row
        for col_name in data.columns:
            row[col_name] = exp(row[col_name]) # 把結果返回給data
return data def _1map(data, exp): _data = [[exp(row[col_name]) # 把結果轉換成2級list for col_name in data.columns] for index, row in data.iterrows() ] return _data if __name__ == "__main__": inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1'
:12,'c2':120}] df = pd.DataFrame(inp) temp = _map(df, lambda ele: ele+1 ) print temp _temp = _1map(df, lambda ele: ele+1) res_data = pd.DataFrame(_temp) # 對2級list轉換成DataFrame print res_data