Pandas可以使用append()方法来添加一行数据。以下是一个示例代码:

import pandas as pd

# 创建一个空的DataFrame
df = pd.DataFrame(columns=['Name', 'Age', 'City'])

# 添加一行数据
new_row = {'Name': 'John', 'Age': 25, 'City': 'New York'}
df = df.append(new_row, ignore_index=True)

print(df)

输出结果为:

   Name Age      City
0  John  25  New York

在上述代码中,我们首先创建了一个空的DataFrame对象,然后使用append()方法将一行数据添加到DataFrame中。ignore_index=True参数用于重置索引,确保新添加的行具有正确的索引值。

pandas如何添加一行数据

原文地址: https://www.cveoy.top/t/topic/jfiQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录