'DataFrame' object has no attribute 'row'
时间: 2023-06-21 15:09:32 浏览: 295
The error message "DataFrame' object has no attribute 'row'" usually occurs when you try to access a row in a pandas DataFrame using the attribute 'row', which does not exist in the DataFrame object.
To access a row in a pandas DataFrame, you can use the .iloc[] or .loc[] method. Here's an example of how to access the first row in a DataFrame using .iloc[]:
```
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
# Access first row using iloc[]
first_row = df.iloc[0]
print(first_row)
```
This will output:
```
A 1
B 3
Name: 0, dtype: int64
```
Alternatively, you can use .loc[] to access a row by label:
```
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}, index=['row1', 'row2'])
# Access row with label 'row1' using loc[]
row1 = df.loc['row1']
print(row1)
```
This will output:
```
A 1
B 3
Name: row1, dtype: int64
```
Note that the output is a pandas Series object, which is similar to a one-dimensional array or dictionary. If you need to access multiple rows, you can use slicing with .iloc[] or .loc[]. For example, to access the first two rows:
```
# Access first two rows using iloc[]
first_two_rows = df.iloc[:2]
print(first_two_rows)
```
This will output:
```
A B
row1 1 3
row2 2 4
```
阅读全文
相关推荐


















