python 中的 pandas 插入方法
本教程解释了如何使用 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.
。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串