Python pandas按行、按列遍历DataFrame的几种方式
作者:琲世
前言
在对DataFrame数据进行处理时,存在需要对数据内容进行遍历的场景。因此记录一下按照行,列遍历的几种方式。
一、按行遍历
1. 使用loc或iloc方法
- loc:表示location,填写内容为行的值或者列表,若填写内容为值,则返回对应行的内容(Series类型);若填写内容为列表,则返回对应行的内容(DataFrame类型)
- iloc:表示integer+location,填写内容为行的索引(int类型)或者列表,返回内容与loc相同。
因此若需要按照行进行遍历时,
- 先可以使用index方法获取索引内容,再使用loc方法
- 先可以使用shape[0]方法获取总行数,再使用iloc方法
2. 使用iterrows()方法
iterrows():按行遍历,将DataFrame的每一行迭代为(index, Series)对,可以通过row[name]对元素进行访问
因此可以直接使用iterrows()方法,获取得到行内容
代码如下:
data = {'a': {'x': [1, 1], 'y': [2, 1], 'z': [3, 1]}, 'b': {'x': [1, 2], 'y': [2, 2], 'z': [3, 2]}, 'c': {'x': [1, 3], 'y': [2, 3], 'z': [3, 3]}} data_pd = pd.DataFrame(data) print(data_pd) for row in data_pd.index: print(data_pd.loc[row]['a']) for row_id in range(data_pd.shape[0]): print(data_pd.iloc[row_id]['a']) for index, row in data_pd.iterrows(): print(row['a'])
运行结果,三种方法结果相同:
a b c
x [1, 1] [1, 2] [1, 3]
y [2, 1] [2, 2] [2, 3]
z [3, 1] [3, 2] [3, 3][1, 1]
[2, 1]
[3, 1]
二、按列遍历
1. 使用列索引方式
DataFrame可以直接使用[列名称]的方式获取列的值,即data_pd['a']
即可得到列内容。
因此若需要按照列进行遍历时,先可以使用colums方法获取列内容,再使用[列名称]方式
2. 使用iteritems()方法
iteritems():按列遍历,将DataFrame的每一列迭代为(列名, Series)对,可以通过row[index]对元素进行访问
因此可以直接使用iteritems()方法,获取得到列内容
代码如下:
data = {'a': {'x': [1, 1], 'y': [2, 1], 'z': [3, 1]}, 'b': {'x': [1, 2], 'y': [2, 2], 'z': [3, 2]}, 'c': {'x': [1, 3], 'y': [2, 3], 'z': [3, 3]}} data_pd = pd.DataFrame(data) print(data_pd) for col in data_pd.columns: print(data_pd[col].iloc[0]) for index, col in data_pd.iteritems(): print(col.iloc[0])
运行结果,两种方法结果相同:
a b c
x [1, 1] [1, 2] [1, 3]
y [2, 1] [2, 2] [2, 3]
z [3, 1] [3, 2] [3, 3][1, 1]
[1, 2]
[1, 3]
补充:遍历dataframe每一行的每一个元素
python遍历的代码,其中df是dataframe类型:
#1. 从mysql读取数据 #"ts_code", "buydate", "buyprice", "selldate", "sellprice", "duration", "strategyid" df = self.dbadapter.QueryBTStrategy(id=1) #2. 统计某一个卖出时间,对应的涨跌幅平均值 print(type(df)) #<class 'pandas.core.frame.DataFrame'> print(type(df.values))#<class 'numpy.ndarray'> print(df.values) #遍历 for row in df.values: print(row[0], ' ', row[1], ' ', row[2])
输出:
<class 'pandas.core.frame.DataFrame'>
<class 'numpy.ndarray'>
[['002089.SZ' '20190416' Decimal('5.75') ... Decimal('6.10') 1 '1']
['002231.SZ' '20190416' Decimal('8.35') ... Decimal('9.19') 1 '1']
['002792.SZ' '20190416' Decimal('35.85') ... Decimal('37.28') 1 '1']
...
['600621.SH' '20190906' Decimal('13.78') ... Decimal('14.15') 1 '1']
['603058.SH' '20190905' Decimal('7.52') ... Decimal('7.77') 2 '1']
['603797.SH' '20190906' Decimal('12.88') ... Decimal('13.94') 1 '1']]
002089.SZ 20190416 5.75
002231.SZ 20190416 8.35
002792.SZ 20190416 35.85
300115.SZ 20190416 13.97
300394.SZ 20190416 34.56
300590.SZ 20190416 42.80
300634.SZ 20190416 30.43
603220.SH 20190416 34.62
300312.SZ 20190416 9.21
002426.SZ 20190417 3.66
300072.SZ 20190418 12.50
603626.SH 20190418 11.34
000413.SZ 20190419 6.99
000530.SZ 20190418 5.24
000972.SZ 20190417 3.79
002147.SZ 20190419 3.49
002297.SZ 20190419 8.66
002316.SZ 20190418 10.03
002436.SZ 20190418 5.77
002778.SZ 20190419 23.00
300128.SZ 20190419 5.80
300136.SZ 20190416 31.24
300160.SZ 20190419 4.58
300366.SZ 20190419 9.78
600773.SH 20190419 8.86
603015.SH 20190418 8.24
603059.SH 20190419 38.19
002600.SZ 20190422 6.31
600499.SH 20190422 5.88
002243.SZ 20190423 22.03
002837.SZ 20190423 21.74
300710.SZ 20190423 33.15
600235.SH 20190423 7.81
600604.SH 20190423 17.58
000050.SZ 20190424 17.38
000997.SZ 20190424 19.33
002387.SZ 20190424 14.01
002870.SZ 20190424 25.95
300097.SZ 20190424 14.84
600186.SH 20190424 2.30
300012.SZ 20190426 9.77
603283.SH 20190426 21.96
600410.SH 20190429 9.73
002698.SZ 20190510 14.32
300578.SZ 20190510 28.73
300472.SZ 20190515 22.13
002112.SZ 20190516 8.35
002496.SZ 20190516 3.63
002621.SZ 20190515 22.87
603899.SH 20190515 39.88
600238.SH 20190520 9.10
600331.SH 20190520 3.50
601208.SH 20190520 5.02
603528.SH 20190521 7.81
000955.SZ 20190522 5.29
002032.SZ 20190522 69.60
002368.SZ 20190522 34.63
300126.SZ 20190522 6.47
300540.SZ 20190522 19.60
600555.SH 20190522 3.22
603496.SH 20190522 29.18
000652.SZ 20190523 4.08
002638.SZ 20190523 3.00
600128.SH 20190523 8.27
600792.SH 20190523 4.22
603638.SH 20190523 21.75
603977.SH 20190523 8.15
002939.SZ 20190524 13.09
002947.SZ 20190524 40.80
603906.SH 20190524 13.93
002084.SZ 20190524 4.98
002370.SZ 20190527 17.84
002666.SZ 20190527 4.98
002943.SZ 20190527 31.64
600117.SH 20190527 4.03
601162.SH 20190527 8.91
002072.SZ 20190528 4.86
002163.SZ 20190528 5.76
002564.SZ 20190527 5.98
002886.SZ 20190528 22.35
002888.SZ 20190527 19.97
002906.SZ 20190527 11.06
600064.SH 20190528 11.17
600523.SH 20190528 13.56
000812.SZ 20190528 4.02
002090.SZ 20190529 20.34
002822.SZ 20190529 5.89
002945.SZ 20190529 11.99
300293.SZ 20190528 8.69
300426.SZ 20190529 7.21
600031.SH 20190528 12.65
600480.SH 20190528 9.87
600635.SH 20190528 6.91
600961.SH 20190529 8.74
603308.SH 20190527 9.97
603377.SH 20190527 17.46
000852.SZ 20190530 9.06
002167.SZ 20190530 7.39
002460.SZ 20190530 25.11
002688.SZ 20190530 5.91
002942.SZ 20190530 29.82
300179.SZ 20190524 4.56
300191.SZ 20190530 18.88
300363.SZ 20190530 8.83
300697.SZ 20190530 13.74
600302.SH 20190529 5.84
603590.SH 20190530 38.58
603727.SH 20190530 16.95
603876.SH 20190530 16.83
000611.SZ 20190531 3.84
000975.SZ 20190531 10.47
002136.SZ 20190528 8.59
002155.SZ 20190531 8.21
002443.SZ 20190531 7.78
002531.SZ 20190531 5.46
002921.SZ 20190531 21.39
300746.SZ 20190531 18.35
600538.SH 20190531 5.45
600871.SH 20190531 2.75
600929.SH 20190530 9.03
601865.SH 20190531 12.23
603031.SH 20190531 13.00
603700.SH 20190531 26.17
603713.SH 20190531 41.30
000544.SZ 20190531 6.23
000961.SZ 20190603 8.98
002636.SZ 20190530 8.19
002828.SZ 20190603 13.28
300208.SZ 20190603 6.40
300501.SZ 20190603 22.32
600525.SH 20190603 5.85
600547.SH 20190603 32.29
601100.SH 20190528 30.35
601319.SH 20190529 9.10
603042.SH 20190603 14.47
603136.SH 20190530 21.63
603559.SH 20190603 21.07
603602.SH 20190603 22.66
603912.SH 20190603 15.08
002422.SZ 20190531 30.16
002848.SZ 20190604 14.10
300678.SZ 20190604 22.16
300716.SZ 20190603 11.42
600311.SH 20190604 4.23
601928.SH 20190531 8.11
002217.SZ 20190604 5.90
002491.SZ 20190605 8.51
002908.SZ 20190605 21.16
300410.SZ 20190603 20.01
300570.SZ 20190605 22.16
300597.SZ 20190605 16.72
300638.SZ 20190605 49.62
002557.SZ 20190605 23.30
002813.SZ 20190610 32.32
000037.SZ 20190611 10.60
002398.SZ 20190611 6.40
002571.SZ 20190611 6.05
300103.SZ 20190611 10.53
603922.SH 20190611 16.80
000633.SZ 20190612 5.56
002464.SZ 20190611 12.25
300339.SZ 20190612 13.23
300357.SZ 20190611 31.86
600864.SH 20190612 6.88
000338.SZ 20190611 12.46
002670.SZ 20190613 11.33
300014.SZ 20190613 25.80
300386.SZ 20190613 12.99
300469.SZ 20190613 23.44
300605.SZ 20190613 20.86
600193.SH 20190613 3.29
600478.SH 20190613 6.09
600698.SH 20190613 2.72
000976.SZ 20190614 5.45
600421.SH 20190614 13.76
600711.SH 20190614 5.56
000545.SZ 20190617 4.29
002511.SZ 20190617 11.00
600456.SH 20190617 23.27
600882.SH 20190617 10.50
300518.SZ 20190618 25.32
603008.SH 20190618 11.18
002384.SZ 20190620 14.75
002672.SZ 20190620 11.77
300262.SZ 20190620 6.80
600501.SH 20190620 9.08
600885.SH 20190620 24.16
601555.SH 20190620 10.52
000716.SZ 20190621 5.79
601388.SH 20190621 2.27
002450.SZ 20190624 2.81
002705.SZ 20190624 10.70
300509.SZ 20190624 9.04
600530.SH 20190624 6.22
600682.SH 20190621 10.79
601218.SH 20190624 3.16
603816.SH 20190624 30.99
002568.SZ 20190624 16.87
002631.SZ 20190625 8.91
300111.SZ 20190625 2.79
300417.SZ 20190621 22.00
600378.SH 20190625 14.92
603185.SH 20190624 42.09
603233.SH 20190624 42.42
603660.SH 20190624 15.53
002184.SZ 20190625 11.70
002589.SZ 20190625 7.67
600026.SH 20190621 6.53
601777.SH 20190626 4.64
603936.SH 20190626 13.39
002175.SZ 20190626 2.27
002388.SZ 20190627 6.86
300353.SZ 20190625 13.77
300601.SZ 20190627 52.15
300655.SZ 20190627 16.18
600408.SH 20190627 2.44
600677.SH 20190625 18.24
603517.SH 20190626 38.35
603800.SH 20190626 13.54
000820.SZ 20190626 2.34
002544.SZ 20190628 13.00
000859.SZ 20190701 5.39
002397.SZ 20190701 5.66
002458.SZ 20190701 20.86
002579.SZ 20190701 10.92
002650.SZ 20190701 3.88
300420.SZ 20190624 5.59
300566.SZ 20190701 17.03
600462.SH 20190626 1.61
000038.SZ 20190702 8.20
002274.SZ 20190702 7.54
300207.SZ 20190701 12.45
300571.SZ 20190701 43.41
600368.SH 20190703 5.22
600614.SH 20190703 2.55
300696.SZ 20190704 29.58
300717.SZ 20190705 17.34
600035.SH 20190705 3.76
603739.SH 20190705 31.81
002100.SZ 20190708 9.13
600191.SH 20190708 6.17
601177.SH 20190708 10.42
300123.SZ 20190708 8.90
603229.SH 20190701 13.30
300387.SZ 20190715 11.09
600281.SH 20190712 4.97
002194.SZ 20190717 15.99
002777.SZ 20190716 27.50
002692.SZ 20190718 2.60
300174.SZ 20190717 21.80
002909.SZ 20190718 10.58
300595.SZ 20190725 36.73
603843.SH 20190729 5.97
300250.SZ 20190801 14.29
300499.SZ 20190801 10.93
300508.SZ 20190729 33.56
600275.SH 20190801 2.46
603110.SH 20190801 16.64
603722.SH 20190801 24.00
600366.SH 20190805 8.10
300542.SZ 20190806 14.80
300745.SZ 20190806 25.10
600083.SH 20190806 13.23
603617.SH 20190808 18.89
300600.SZ 20190809 10.63
300726.SZ 20190812 26.03
002786.SZ 20190815 7.66
603583.SH 20190815 34.83
002781.SZ 20190812 18.52
300556.SZ 20190819 17.80
600127.SH 20190815 5.18
002168.SZ 20190820 8.60
601066.SH 20190820 18.80
002192.SZ 20190821 16.71
300081.SZ 20190821 8.55
300675.SZ 20190821 16.29
600081.SH 20190821 9.86
600217.SH 20190821 5.86
600745.SH 20190821 42.85
002130.SZ 20190822 4.76
002229.SZ 20190822 6.90
002325.SZ 20190821 3.13
300379.SZ 20190820 21.20
300449.SZ 20190822 9.02
603520.SH 20190822 23.57
002437.SZ 20190823 3.48
600903.SH 20190823 11.68
002114.SZ 20190823 8.01
002509.SZ 20190827 1.74
002662.SZ 20190826 2.88
002743.SZ 20190827 5.95
300431.SZ 20190827 5.36
300107.SZ 20190829 8.09
300351.SZ 20190827 8.71
600800.SH 20190826 5.48
002077.SZ 20190830 6.24
002570.SZ 20190827 5.28
300455.SZ 20190830 9.18
002395.SZ 20190902 24.35
300362.SZ 20190830 3.40
300598.SZ 20190902 34.82
300629.SZ 20190902 21.20
300768.SZ 20190902 38.52
000727.SZ 20190903 2.33
002467.SZ 20190903 5.94
002941.SZ 20190903 22.61
600198.SH 20190903 11.67
002195.SZ 20190903 3.50
300637.SZ 20190904 12.88
603078.SH 20190903 27.62
000063.SZ 20190905 30.65
600776.SH 20190905 22.46
000586.SZ 20190906 12.86
000890.SZ 20190906 6.44
002396.SZ 20190906 27.58
300260.SZ 20190904 12.53
300560.SZ 20190906 18.47
300763.SZ 20190904 39.21
600094.SH 20190906 7.79
600352.SH 20190905 14.88
600621.SH 20190906 13.78
603058.SH 20190905 7.52
603797.SH 20190906 12.88
回测用时(秒): 0
总结
到此这篇关于Python pandas按行、按列遍历DataFrame的文章就介绍到这了,更多相关pandas按行列遍历DataFrame内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!