如何在 pandas dataframe 中创建一个空列
我们可以使用 dataframe 对象的 reindex()
,assign()
和 insert()
方法在 pandas 中向 dataframe 添加一个空列。我们还可以直接为 dataframe 的列分配一个空值,以在 pandas 中创建一个空列。
通过简单的分配创建 pandas 空的列
我们可以直接将 dataframe 的列分配给空字符串,nan
值或空 pandas series
,以在 pandas 中创建一个空列。
import pandas as pd
import numpy as np
dates = ["april-20", "april-21", "april-22", "april-23", "april-24", "april-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.dataframe({"date": dates, "income": income, "expenses": expenses})
df["empty_1"] = ""
df["empty_2"] = np.nan
df["empty_3"] = pd.series()
print(df)
输出:
date income expenses empty_1 empty_2 empty_3
0 april-20 10 3 nan nan
1 april-21 20 8 nan nan
2 april-22 10 4 nan nan
3 april-23 15 5 nan nan
4 april-24 10 6 nan nan
5 april-25 12 10 nan nan
它在 df 中创建三个空列。为 empty_1
列分配空字符串,为 empty_2
分配 nan 值,为 empty_3
分配一个空 pandas series
,这也将导致整个 empty_3
的值为 nan。
在 pandas 中用 pandas.dataframe.reindex()
方法添加一个空列
我们可以使用 pandas.dataframe.reindex() 方法向 pandas 中的 dataframe 添加多个空列。
import pandas as pd
import numpy as np
dates = ["april-20", "april-21", "april-22", "april-23", "april-24", "april-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.dataframe({"date": dates, "income": income, "expenses": expenses})
column_names = ["empty_1", "empty_2", "empty_3"]
df = df.reindex(columns=column_names)
print(df)
输出:
empty_1 empty_2 empty_3
0 nan nan nan
1 nan nan nan
2 nan nan nan
3 nan nan nan
4 nan nan nan
5 nan nan nan
该代码在 df 中使用所有 nan 值创建了新列 empty_1
,empty_2
,empty_3
,而所有旧信息都丢失了。
要在保留初始列的同时添加多个新列,我们可以编写如下代码:
import pandas as pd
import numpy as np
dates = ["april-20", "april-21", "april-22", "april-23", "april-24", "april-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.dataframe({"date": dates, "income": income, "expenses": expenses})
df = df.reindex(columns=df.columns.tolist() ["empty_1", "empty_2", "empty_3"])
print(df)
输出:
date income expenses empty_1 empty_2 empty_3
0 april-20 10 3 nan nan nan
1 april-21 20 8 nan nan nan
2 april-22 10 4 nan nan nan
3 april-23 15 5 nan nan nan
4 april-24 10 6 nan nan nan
5 april-25 12 10 nan nan nan
这样会在保留初始信息的情况下向 df 添加空列 empty_1
,empty_2
和 empty_3
。
pandas.dataframe.assign()
在 pandas dataframe 中添加一个空列
我们可以使用 pandas.dataframe.assign() 方法向其中 pandas 中的 dataframe 添加一个空列。
import pandas as pd
import numpy as np
dates = ["april-20", "april-21", "april-22", "april-23", "april-24", "april-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.dataframe({"date": dates, "income": income, "expenses": expenses})
df = df.assign(empty_1="", empty_2=np.nan)
print(df)
输出:
date income expenses empty_1 empty_2
0 april-20 10 3 nan
1 april-21 20 8 nan
2 april-22 10 4 nan
3 april-23 15 5 nan
4 april-24 10 6 nan
5 april-25 12 10 nan
它将创建一个名为 empty_1
和 empty_2
的空列,仅在 df
中包含 nan 值。
pandas.dataframe.insert()
将空列添加到 dataframe
pandas.dataframe.insert() 允许我们在 dataframe 中插入列指定位置。我们可以使用此方法向 dataframe
添加一个空列。
语法:
dataframe.insert(loc, column, value, allow_duplicates=false)
它在位置 loc
处创建一个名称为 column
的新列,默认值为 value
。allow_duplicates = false
确保 dataframe
中只有一列名为 column
的列。如果我们传递一个空字符串或 nan
值作为值参数,则可以向 dataframe 添加一个空列。
import pandas as pd
import numpy as np
dates = ["april-20", "april-21", "april-22", "april-23", "april-24", "april-25"]
income = [10, 20, 10, 15, 10, 12]
expenses = [3, 8, 4, 5, 6, 10]
df = pd.dataframe({"date": dates, "income": income, "expenses": expenses})
df.insert(3, "empty_1", "")
df.insert(4, "empty_2", np.nan)
print(df)
输出:
date income expenses empty_1 empty_2
0 april-20 10 3 nan
1 april-21 20 8 nan
2 april-22 10 4 nan
3 april-23 15 5 nan
4 april-24 10 6 nan
5 april-25 12 10 nan
它在 df
中创建 empty_1
列,并在索引 3
处创建所有空值,并在索引 4
处创建具有所有 nan
值的 empty_2
。
转载请发邮件至 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 系列日期时间转换为字符串