我正在尝试使用以下代码循环遍历 Polars 记录集:
import polars as pl
mydf = pl.DataFrame(
{"start_date": ["2020-01-02", "2020-01-03", "2020-01-04"],
"Name": ["John", "Joe", "James"]})
print(mydf)
│start_date ┆ Name │
│ --- ┆ --- │
│ str ┆ str │
╞════════════╪═══════╡
│ 2020-01-02 ┆ John │
│ 2020-01-03 ┆ Joe │
│ 2020-01-04 ┆ James │
for row in mydf.rows():
print(row)
('2020-01-02', 'John')
('2020-01-03', 'Joe')
('2020-01-04', 'James')
有没有一种方法可以使用命名列而不是索引来特定地引用“Name”?在Pandas中,这看起来像:
import pandas as pd
mydf = pd.DataFrame(
{"start_date": ["2020-01-02", "2020-01-03", "2020-01-04"],
"Name": ["John", "Joe", "James"]})
for index, row in mydf.iterrows():
mydf['Name'][index]
'John'
'Joe'
'James'
for row in mydf.iterrows(named=True): row['Name']
,我会得到错误信息Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: tuple indices must be integers or slices, not str
。 - John Smith之前的版本返回namedtuple而不是dict。
请尝试row.Name
。 - 0x26res