要在pandas中添加一行新数据,可以使用DataFrame.append()方法。下面是一个示例:

import pandas as pd

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

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

print(df)

这将输出以下结果:

   Name Age      City
0  John  25  New York

在这个示例中,我们首先创建了一个空的DataFrame,然后使用append()方法将一行新数据添加到DataFrame中。请注意,ignore_index=True参数用于重新索引DataFrame,确保新行具有递增的索引值。

pandas 添加一行新数据

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

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