python 中的 pandas 插入方法-ag捕鱼王app官网

python 中的 pandas 插入方法

作者:迹忆客 最近更新:2024/04/23 浏览次数:

本教程解释了如何使用 insert() 方法在 pandas dataframe 中插入一列。

import pandas as pd
countries_df = pd.dataframe(
    {
        "country": ["nepal", "switzerland", "germany", "canada"],
        "continent": ["asia", "europe", "europe", "north america"],
        "primary language": ["nepali", "french", "german", "english"],
    }
)
print("countries dataframe:")
print(countries_df, "\n")

输出:

countries dataframe:
       country      continent primary language
0        nepal           asia           nepali
1  switzerland         europe           french
2      germany         europe           german
3       canada  north america          english

我们将使用上例中所示的 countries_df dataframe 来解释如何使用 insert() 方法在 pandas dataframe 中插入一列。


pandas.dataframe.insert() 方法

语法

dataframe.insert(loc, column, value, allow_duplicates=false)

它将名为 column 的列插入到 dataframe 中,其值由 value 指定,位于 loc 位置。

使用 insert() 方法插入对所有行具有相同值的列

import pandas as pd
countries_df = pd.dataframe(
    {
        "country": ["nepal", "switzerland", "germany", "canada"],
        "continent": ["asia", "europe", "europe", "north america"],
        "primary language": ["nepali", "french", "german", "english"],
    }
)
print("countries dataframe:")
print(countries_df, "\n")
countries_df.insert(3, "capital", "unknown")
print("countries dataframe after inserting capital column:")
print(countries_df)

输出:

countries dataframe:
       country      continent primary language
0        nepal           asia           nepali
1  switzerland         europe           french
2      germany         europe           german
3       canada  north america          english
countries dataframe after inserting capital column:
       country      continent primary language  capital
0        nepal           asia           nepali  unknown
1  switzerland         europe           french  unknown
2      germany         europe           german  unknown
3       canada  north america          english  unknown

它在 countries_df dataframe 的 3 索引位置插入 ·capital·列,所有行的 ·capital·列值均设置为 unknown

该位置从 0 开始,因此 3 位置指的是 dataframe 中的 4 列。

在 dataframe 中插入一列,指定每行的值

如果我们想使用 insert() 方法为要插入的列指定每一行的值,我们可以在 insert() 方法中传递一个值列表作为 value 参数。

import pandas as pd
countries_df = pd.dataframe(
    {
        "country": ["nepal", "switzerland", "germany", "canada"],
        "continent": ["asia", "europe", "europe", "north america"],
        "primary language": ["nepali", "french", "german", "english"],
    }
)
print("countries dataframe:")
print(countries_df, "\n")
capitals = ["kathmandu", "zurich", "berlin", "ottawa"]
countries_df.insert(2, "capital", capitals)
print("countries dataframe after inserting capital column:")
print(countries_df)

输出:

countries dataframe:
       country      continent primary language
0        nepal           asia           nepali
1  switzerland         europe           french
2      germany         europe           german
3       canada  north america          english
countries dataframe after inserting capital column:
       country      continent    capital primary language
0        nepal           asia  kathmandu           nepali
1  switzerland         europe     zurich           french
2      germany         europe     berlin           german
3       canada  north america     ottawa          english

它在 dataframe countries_df 中的索引 2 插入了列 capital,并为 dataframe 中的 capital 列指定了每一行的值。


insert() 方法中设置 allow_duplicates = true 来添加已经存在的列

import pandas as pd
countries_df = pd.dataframe(
    {
        "country": ["nepal", "switzerland", "germany", "canada"],
        "continent": ["asia", "europe", "europe", "north america"],
        "primary language": ["nepali", "french", "german", "english"],
        "capital": ["kathmandu", "zurich", "berlin", "ottawa"],
    }
)
print("countries dataframe:")
print(countries_df, "\n")
capitals = ["kathmandu", "zurich", "berlin", "ottawa"]
countries_df.insert(4, "capital", capitals, allow_duplicates=true)
print("countries dataframe after inserting capital column:")
print(countries_df)

输出:

countries dataframe:
       country      continent primary language    capital
0        nepal           asia           nepali  kathmandu
1  switzerland         europe           french     zurich
2      germany         europe           german     berlin
3       canada  north america          english     ottawa
countries dataframe after inserting capital column:
       country      continent primary language    capital    capital
0        nepal           asia           nepali  kathmandu  kathmandu
1  switzerland         europe           french     zurich     zurich
2      germany         europe           german     berlin     berlin
3       canada  north america          english     ottawa     ottawa

它将列 capital 添加到 countries_df dataframe 中,尽管 countries_df dataframe 中已经存在 capital 列。

如果我们尝试插入已经存在于 dataframe 中的列,而没有在 insert() 方法中设置 allow_duplicates = true,它就会向我们抛出一个错误信息:valueerror: cannot insert column, already exists.

上一篇:

下一篇:如何获取 pandas dataframe 的行数

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图