您现在的位置是:网站首页> 编程资料编程资料

Python pandas按行、按列遍历DataFrame的几种方式_python_

2023-05-26 429人已围观

简介 Python pandas按行、按列遍历DataFrame的几种方式_python_

前言

在对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)) # print(type(df.values))# print(df.values) #遍历 for row in df.values: print(row[0], ' ', row[1], ' ', row[2])

输出:



[['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

-六神源码网