pandas 添加一行数据
要在pandas中添加一行数据,可以使用append()方法。以下是一个示例:
import pandas as pd
# 创建一个空的DataFrame
df = pd.DataFrame(columns=['Name', 'Age', 'City'])
# 添加一行数据
new_row = {'Name': 'John', 'Age': 30, 'City': 'New York'}
df = df.append(new_row, ignore_index=True)
print(df)
输出:
Name Age City
0 John 30 New York
在这个示例中,我们首先创建一个空的DataFrame,然后创建一个包含新数据的字典new_row。然后,我们使用append()方法将新行添加到DataFrame中,并将ignore_index参数设置为True,以确保新行的索引正确。
你可以根据需要添加多个行,只需重复上述步骤即可。
原文地址: https://www.cveoy.top/t/topic/hUj8 著作权归作者所有。请勿转载和采集!