[英]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.