可以使用 append() 方法来添加一行数据到 DataFrame 中。首先,创建一个字典,包含新行的数据。然后,使用 append() 方法将字典添加到 DataFrame 中,将结果赋值给一个新的 DataFrame。

以下是一个示例:

import pandas as pd

# 创建一个示例 DataFrame
data = {'A': [1, 2, 3],
        'B': [4, 5, 6]}
df = pd.DataFrame(data)

# 创建一个新行的数据
new_row = {'A': 7, 'B': 8}

# 使用 append() 方法将新行添加到 DataFrame 中
df_new = df.append(new_row, ignore_index=True)

在上面的示例中,我们首先创建了一个 DataFrame df,然后创建了一个新行的数据 new_row。最后,使用 append() 方法将 new_row 添加到了 df 中,将结果赋值给一个新的 DataFrame df_new

Python Pandas DataFrame 添加一行数据 - append() 方法

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

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