将空列添加到 pandas dataframe
pandas 还提供了一项功能,可以将一个或多个空列添加到 dataframe
(表)。我们可以通过多种方式轻松地在 pandas dataframe
中添加空列。
我们将在本教程中展示如何使用各种方法在 pandas dataframe
中添加一个或多个空列,例如使用 assignment operator
和使用 assign()
, insert()
, reindex()
和 apply()
方法。我们还将展示每种方法的实现,以简要说明每种方法的工作原理。
使用 赋值运算符
在 pandas dataframe
中添加空列
使用 赋值运算符
或 空字符串
,我们可以在 pandas dataframe
中添加空列。使用这种方法,将空值或 nan
值分配给 dataframe
中的任何列。
在下面的示例中,我们创建了一个 dataframe
,然后使用 assignment operator
,我们将空字符串和 nan
值分配给两个新添加的列,就像 pandas dataframe
中一样。这些列是地址
和名称
。使用 numpy
库,我们将把 nan
值导入到 dataframe
列中。
让我们看看如何使用 赋值运算符
或 空字符串
将空列添加到 pandas 中的 dataframe
。
示例代码:
import pandas as pd
import numpy as np
company_data = {
"employee name": ["samreena", "mirha", "asif", "raees"],
"employee id": [101, 102, 103, 104],
}
dataframe = pd.dataframe(company_data)
print("------------ original dataframe --------------\n", dataframe)
# add empty column using assignment operator
dataframe["blank_column"] = " "
dataframe["address"] = np.nan
dataframe["designation"] = none
print("------------ after adding empty columns ---------------\n", dataframe)
输出:
------------ original dataframe --------------
employee name employee id
0 samreena 101
1 mirha 102
2 asif 103
3 raees 104
------------ after adding empty columns ---------------
employee name employee id designation address
0 samreena 101 nan
1 mirha 102 nan
2 asif 103 nan
3 raees 104 nan
使用 dataframe.assign()
方法在 pandas dataframe
中添加一个空列
dataframe.assign() 方法用于向 dataframe
添加一列或多列。在现有 pandas dataframe
中添加新的空列后,在 dataframe
上应用 assign()
方法会返回一个新的 dataframe
。
示例代码:
import pandas as pd
import numpy as np
company_data = {
"employee name": ["samreena", "mirha", "asif", "raees"],
"employee id": [101, 102, 103, 104],
}
dataframe1 = pd.dataframe(company_data)
print("------------ original dataframe --------------\n", dataframe1)
# add empty column into the dataframe using assign() method
dataframe2 = dataframe1.assign(designation=" ", empty_column=np.nan, address=none)
print("------------ after adding empty columns ---------------\n", dataframe2)
输出:
------------ original dataframe --------------
employee name employee id
0 samreena 101
1 mirha 102
2 asif 103
3 raees 104
------------ after adding empty columns ---------------
employee name employee id designation empty_column address
0 samreena 101 nan none
1 mirha 102 nan none
2 asif 103 nan none
3 raees 104 nan none
使用 dataframe.reindex()
方法在 pandas dataframe
中添加一个空列
dataframe.reindex()
方法将 nan
值分配给 pandas dataframe
中的空列。这个 reindex()
方法获取现有和新添加的列的列表。使用这种方法,我们可以将任何索引位置的空列添加到 dataframe
。
在以下示例中,我们创建了一个新的 dataframe
,其中包含两列名称,分别为 employee name
和 employee id
。后来,我们使用 dataframe.reindex()
方法将另外两个新列 address
和 designation
添加到具有指定 nan
值的列列表中。
示例代码:
import pandas as pd
import numpy as np
company_data = {
"employee name": ["samreena", "mirha", "asif", "raees"],
"employee id": [101, 102, 103, 104],
}
dataframe1 = pd.dataframe(company_data)
print("------------ original dataframe --------------\n", dataframe1)
# pandas add empty columns to the dataframe using reindex() method
dataframe2 = dataframe1.reindex(
columns=dataframe1.columns.tolist() ["designation", "address"]
)
print("------------ after adding empty columns ---------------\n", dataframe2)
输出:
------------ original dataframe --------------
employee name employee id
0 samreena 101
1 mirha 102
2 asif 103
3 raees 104
------------ after adding empty columns ---------------
employee name employee id designation address
0 samreena 101 nan nan
1 mirha 102 nan nan
2 asif 103 nan nan
3 raees 104 nan nan
使用 dataframe.insert()
方法在 pandas dataframe
中添加一个空列
dataframe.insert()
方法在 pandas dataframe
的任何索引位置(开始、中间、结束或指定位置)插入一个空列。
示例代码:
import pandas as pd
import numpy as np
company_data = {
"employee name": ["samreena", "mirha", "asif", "raees"],
"employee id": [101, 102, 103, 104],
}
dataframe = pd.dataframe(company_data)
print("------------ original dataframe --------------\n", dataframe)
# pandas add empty columns to the dataframe using insert() method
dataframe.insert(1, "designation", "")
print("------------ after adding empty columns ---------------\n", dataframe)
输出:
------------ original dataframe --------------
employee name employee id
0 samreena 101
1 mirha 102
2 asif 103
3 raees 104
------------ after adding empty columns ---------------
employee name designation employee id
0 samreena 101
1 mirha 102
2 asif 103
3 raees 104
使用 dataframe.apply()
方法在 pandas dataframe
中添加一个空列
使用 dataframe.apply() 方法 和 lambda
函数,我们还可以向 pandas dataframe
添加空列。请参阅以下示例,使用 dataframe.apply()
方法将空列添加到 pandas 中的 dataframe
。
示例代码:
import pandas as pd
import numpy as np
company_data = {
"employee name": ["samreena", "mirha", "asif", "raees"],
"employee id": [101, 102, 103, 104],
}
dataframe = pd.dataframe(company_data)
print("------------ original dataframe --------------\n", dataframe)
# pandas add empty columns to the dataframe using apply() method
dataframe["empty_column"] = dataframe.apply(lambda _: " ", axis=1)
print("------------ after adding empty columns ---------------\n", dataframe)
输出:
------------ original dataframe --------------
employee name employee id
0 samreena 101
1 mirha 102
2 asif 103
3 raees 104
------------ after adding empty columns ---------------
employee name employee id empty_column
0 samreena 101
1 mirha 102
2 asif 103
3 raees 104
结论
在本教程中,我们介绍了不同的方法,例如 assign()
、insert()
、apply()
和 reindex()
以在 pandas dataframe
中添加一个或多个空列。我们还展示了如何使用 赋值运算符
将空列添加到 dataframe
。
转载请发邮件至 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 系列日期时间转换为字符串