python pandas添加行
时间: 2024-05-20 18:09:43 浏览: 96
要在 Pandas 中添加行,可以使用 append() 函数。以下是添加行的几种方法:
1. 通过字典添加一行数据
可以使用字典来创建一个新的 DataFrame,并将其附加到现有的 DataFrame 中。示例如下:
```python
import pandas as pd
# 创建一个空的 DataFrame
df = pd.DataFrame(columns=['Name', 'Age'])
# 使用字典添加一行数据
new_data = {'Name': 'John', 'Age': 25}
df = df.append(new_data, ignore_index=True)
```
2. 通过列表添加多行数据
可以使用列表来创建一个新的 DataFrame,并将其附加到现有的 DataFrame 中。示例如下:
```python
import pandas as pd
# 创建一个空的 DataFrame
df = pd.DataFrame(columns=['Name', 'Age'])
# 使用列表添加多行数据
new_data = [['John', 25], ['Jane', 30]]
df = df.append(pd.DataFrame(new_data, columns=['Name', 'Age']), ignore_index=True)
```
3. 使用 loc[] 添加一行数据
可以使用 loc[] 函数来添加一行数据。示例如下:
```python
import pandas as pd
# 创建一个空的 DataFrame
df = pd.DataFrame(columns=['Name', 'Age'])
# 使用 loc[] 添加一行数据
df.loc[len(df)] = ['John', 25]
```
阅读全文
相关推荐


















