简体   繁体   English

如何按列和行遍历数据表?

[英]How to iterate through a data table by column and row?

I want to calculate growth for a data table.我想计算数据表的增长。

I am stuck on iterating through the rows and columns.我坚持遍历行和列。

DF:东风:

City ...... year(2001) ... 2002 ... 2003
New York... 20.0.... 24.1 ... 28.7
Los Angeles.11.1.... 12.1 ... 13.1

New Table:新表:

City ...... 2001 ... 2002 ... 2003
New York... nan....  0.20 ...  0.19
Los Angeles.nan....  0.10 ...   0.8

Code:代码:

for row in range(len(import_table)):
  for i in column_list:
    if i == "_2000":
         pass
    else:
        calculated_growth =  import_table.iloc[i].sub(import_table.iloc[i-1])
      ## update new DF ##
  column_calc_position = column_calc_position + 1

This is a lot harder in python than it seems.这在 python 中比看起来要困难得多。 I'm teaching myself data science by re-coding something I wrote in VBA (and Excel).我通过重新编码我在 VBA(和 Excel)中写的东西来自学数据科学。

Iterate through rows:遍历行:

for row in df.rows:
   print(row['2001'], row['2002'])

Iterate through column:遍历列:

for column in df:
    print(df[column])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中遍历数据框中的一列,得到该行对应的算术计算 - How to iterate through a column in data frame in python and get the arithmetic calculation corresponding to the row 如何遍历 pandas dataframe 中的列 x 行组合 - How to iterate through column x row combinations in a pandas dataframe 遍历表格行图像,然后单击超链接 - Iterate through table row images and click on hyperlink 如何遍历 pyspark 中未知数据帧的列的行 - How to iterate through rows of a column of a unknown data-frame in pyspark 如何遍历数据框单列中的行? - how to iterate through rows within single column of data frame? 如何迭代此数据框 - 第一行没有第 1 列 - How do I iterate this Data frame - First row has no column 1 我是否需要遍历每一行数据来计算每列类别的时间? - Do I need to iterate through every row of data to calculate time per column category? 如何将每个列名和 append 迭代到 python 中的表或数据框 - How to iterate for each column name and append it to a table or data frame in python 遍历数据集中的特定列 - Iterate through specific column in data set 遍历 dataframe 中的每一行和每一列并对列值执行操作 - iterate through each row and column in dataframe and perform action on column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM