为什么使用for循环的时候需要把index, row in data frame.iterrows() in pandas
Why do you need to put index, row in data frame.iterrows() in pandas when using a for loop?
我试过 运行 这个代码:
import pandas
data = pandas.read_csv("data.csv")
for row in data.iterrows():
print(row["column_title"])
我一直收到类型错误:
File "main.py", line 4, in <module>
print(row["column_title_1"])
TypeError: tuple indices must be integers or slices, not str
Whosebug上关于iterrows的问题用的是这个:
for index, row in data.iterrows()
为什么必须添加它才能使代码正常工作?
Pandas iterrows returns 包含行的 index
和 Series
的 tuple
,如文档所述。
Iterate over DataFrame rows as (index, Series) pairs.
如果您想访问该系列,您需要先使用您提到的解包语法对 data.iterrows()
的结果进行解包。
我试过 运行 这个代码:
import pandas
data = pandas.read_csv("data.csv")
for row in data.iterrows():
print(row["column_title"])
我一直收到类型错误:
File "main.py", line 4, in <module>
print(row["column_title_1"])
TypeError: tuple indices must be integers or slices, not str
Whosebug上关于iterrows的问题用的是这个:
for index, row in data.iterrows()
为什么必须添加它才能使代码正常工作?
Pandas iterrows returns 包含行的 index
和 Series
的 tuple
,如文档所述。
Iterate over DataFrame rows as (index, Series) pairs.
如果您想访问该系列,您需要先使用您提到的解包语法对 data.iterrows()
的结果进行解包。