for
遍历后,Series 可直接获取相应的 value,而 DataFrame 则会获取列标签。示例如下:import pandas as pd import numpy as np N=20 df = pd.DataFrame({ 'A': pd.date_range(start='2016-01-01',periods=N,freq='D'), 'x': np.linspace(0,stop=N-1,num=N), 'y': np.random.rand(N), 'C': np.random.choice(['Low','Medium','High'],N).tolist(), 'D': np.random.normal(100, 10, size=(N)).tolist() }) print(df) for col in df: print (col)输出结果:
A x y C D
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns=['col1','col2','col3']) for key,value in df.iteritems(): print (key,value)输出结果:
col1 0 0.561693 1 0.537196 2 0.882564 3 1.063245 Name: col1, dtype: float64 col2 0 -0.115913 1 -0.526211 2 -1.232818 3 -0.313741 Name: col2, dtype: float64 col3 0 0.103138 1 -0.655187 2 -0.101757 3 1.505089 Name: col3, dtype: float64
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(3,3),columns = ['col1','col2','col3']) print(df) for row_index,row in df.iterrows(): print (row_index,row)
col1 col2 col3 0 -0.319301 0.205636 0.247029 1 0.673788 0.874376 1.286151 2 0.853439 0.543066 -1.759512 0 col1 -0.319301 col2 0.205636 col3 0.247029 Name: 0, dtype: float64 1 col1 0.673788 col2 0.874376 col3 1.286151 Name: 1, dtype: float64 2 col1 0.853439 col2 0.543066 col3 -1.759512 Name: 2, dtype: float64注意:iterrows() 遍历行,其中 0,1,2 是行索引而 col1,col2,col3 是列索引。
import pandas as pd import numpy as np df = pd.DataFrame(np.random.rand(3,3),columns = ['c1','c2','c3']) for row in df.itertuples(): print(row)输出结果:
Pandas(Index=0, c1=0.253902385555437, c2=0.9846386610838339, c3=0.8814786409138894) Pandas(Index=1, c1=0.018667367298908943, c2=0.5954745800963542, c3=0.04614488622991075) Pandas(Index=2, c1=0.3066297875412092, c2=0.17984210928723543, c3=0.8573031941082285)
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(3,3),columns = ['col1','col2','col3']) for index, row in df.iterrows(): row['a'] = 15 print (df)输出结果:
col1 col2 col3 0 1.601068 -0.098414 -1.744270 1 -0.432969 -0.233424 0.340330 2 -0.062910 1.413592 0.066311由上述示例可见,原对象
df
没有受到任何影响。
本文链接:http://task.lmcjl.com/news/17262.html