Pandas教程之二十五:Pandas dataframe.append()

Python – Pandas dataframe.append()

Pandas 的append函数用于将其他数据框的行添加到现有数据框的末尾,并返回一个新的数据框对象。原始数据框中不存在的列将添加为新列,新单元格将填充 NaN 值。

将 Dataframe 附加到另一个 Dataframe 中

在这个例子中,我们创建两个数据框,并使用 df.append() 将第二个数据框附加到第一个数据框。

Python

# Importing pandas as pd
import pandas as pd

# Creating the first Dataframe using dictionary
df1 = pd.DataFrame({"a":[1, 2, 3, 4],"b":[5, 6, 7, 8]})

# Creating the Second Dataframe using dictionary
df2 = pd.DataFrame({"a":[1, 2, 3],"b":[5, 6, 7]})

# to append df2 at the end of df1 dataframe
print(df1.append(df2))

Pandas dataframe append 语法

语法:  DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)

参数:

  • other DataFrame 或 Series/dict 类对象,或者这些对象的列表 
  • ignore_index:如果为 True,则不使用索引标签。 
  • verify_integrity:如果为 True,则在创建具有重复的索引时引发 ValueError。 
  • sortPandas如果自身和其它的列未对齐,则对列进行排序。默认排序已弃用,并将在 Pandas 的未来版本中更改为不排序。明确传递 sort=True 以消除警告并进行排序。明确传递 sort=False 以消除警告并且不进行排序。 

返回类型:appended : DataFrame

注意:从 Pandas 2.0 版开始,Pandas append() 方法不再使用。使用 Pandas 时务必记住这一点。连接 DataFrames 的更有效的替代方法是使用pandas.DataFrame模块中的 .concat() 函数。

Pandas 附加两个 DataFrames 忽略索引

注意,第二个数据框的索引值在附加数据框中保留。如果我们不想发生这种情况,那么我们可以设置 ignore_index=True。 

Python

# A continuous index value will be maintained
# across the rows in the new appended data frame.
df1.append(df2, ignore_index=True)

输出

   a  b
0  1  5
1  2  6
2  3  7
3  4  8
4  1  5
5  2  6
6  3  7

将行附加到数据框

在这个例子中,我们将字典作为行附加到数据框。

Python

import pandas as pd

# Creating the first Dataframe using dictionary
df1 = df = pd.DataFrame({"a": [1, 2, 3, 4],
                         "b": [5, 6, 7, 8]})

# Append Dict as row to DataFrame
new_row = {"a": 10, "b": 10}
df2 = df.append(new_row, ignore_index=True)

print(df2)

输出

    a   b 
0   1   5 
1   2   6 
2   3   7 
3   4   8 
4  10  10

附加到不同形状的数据框

在此示例中,我们附加了不同形状的数据框。如果数据框中的列数不相等,则其中一个数据框中不存在的值将用 NaN 值填充。 

Python

# Importing pandas as pd
import pandas as pd

# Creating the first Dataframe using dictionary
df1 = pd.DataFrame({"a":[1, 2, 3, 4],
                    "b":[5, 6, 7, 8]})

# Creating the Second Dataframe using dictionary
df2 = pd.DataFrame({"a":[1, 2, 3],
                    "b":[5, 6, 7],
                    "c":[1, 5, 4]})

# for appending df2 at the end of df1
df1 = df1.append(df2, ignore_index = True)
df1

输出

   a  b    c
0  1  5  NaN
1  2  6  NaN
2  3  7  NaN
3  4  8  NaN
4  1  5  1.0
5  2  6  5.0
6  3  7  4.0

请注意,新单元格填充了 NaN 值。

Python – Pandas dataframe.append() – 常见问题解答

Pandas DataFrame 中的 append 方法是什么?

Pandas DataFrame 中的append()方法用于将一个 DataFrame 的行附加到另一个 DataFrame。它返回一个包含附加行的新 DataFrame。

如何将一个 DataFrame 附加到另一个 DataFrame ?

您可以使用Pandas 中的“append()”方法将一个 DataFrame 附加到另一个 DataFrame。操作方法如下:

import pandas as pd

# Example DataFrames
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

# Append df2 to df1
df_appended = df1.append(df2, ignore_index=True)

print(df_appended)

输出:

   A  B
0  1  3
1  2  4
2  5  7
3  6  8

'ignore_index=True'确保在附加后重置索引,从而提供连续的索引范围。

如何将结果附加到 Pandas 中的 DataFrame ?

如果要将单行或多行附加到现有的 DataFrame,则可以使用带有新索引的 loc 访问器:

import pandas as pd

# Existing DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

# Append a new row
new_row = {'A': 5, 'B': 6}
df = df.append(new_row, ignore_index=True)

print(df)

输出:

   A  B
0  1  3
1  2  4
2  5  6

如何在 DataFrame 中附加列表?

如果您有一个列表并希望将其作为新行附加到 DataFrame 中,则可以将列表转换为 DataFrame,然后将其附加:

import pandas as pd

# Existing DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

# List to append
new_data = [5, 6]

# Convert list to DataFrame and append
df = df.append(pd.Series(new_data, index=df.columns), ignore_index=True)

print(df)

输出:

   A  B
0  1  3
1  2  4
2  5  6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潜洋

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值