如何基于 pandas 中的给定条件创建 dataframe 列
我们可以使用 dataframe 对象的列表推导,numpy 方法,apply()
方法和 map()
方法根据 pandas 中的给定条件创建 dataframe
列。
列表推导以根据 pandas 中的给定条件创建新的 dataframe
列
我们可以根据 pandas 中的给定条件,利用各种列表推导来创建新的 dataframe
列。列表推导是一种从可迭代对象创建新列表的方法。它比其他方法更快,更简单。
import pandas as pd
import numpy as np
list_of_dates = [
"2019-11-20",
"2020-01-02",
"2020-02-05",
"2020-03-10",
"2020-04-16",
"2020-05-01",
]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry", "kevin"]
salary = [200, 400, 300, 500, 600, 300]
df = pd.dataframe(
{"name": employees, "joined date": pd.to_datetime(list_of_dates), "salary": salary}
)
df["status"] = ["senior" if s >= 400 else "junior" for s in df["salary"]]
print(df)
输出:
name joined date salary status
0 hisila 2019-11-20 200 junior
1 shristi 2020-01-02 400 senior
2 zeppy 2020-02-05 300 junior
3 alina 2020-03-10 500 senior
4 jerry 2020-04-16 600 senior
5 kevin 2020-05-01 300 junior
如果 salary
大于或等于 400,它将在 df
中创建一个新列 status
,其值将为 senior
,否则为 junior
。
numpy 方法根据 pandas 中的给定条件创建新的 dataframe 列
我们还可以使用 numpy 方法根据 pandas 中的给定条件创建一个 dataframe
列。为此,我们可以使用 np.where()
方法和 np.select()
方法。
np.where()
方法
np.where()
将条件作为输入并返回满足给定条件的元素的索引。当我们只有一个条件时,可以使用此方法根据 pandas 中的给定条件创建 dataframe 列。
import pandas as pd
import numpy as np
list_of_dates = [
"2019-11-20",
"2020-01-02",
"2020-02-05",
"2020-03-10",
"2020-04-16",
"2020-05-01",
]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry", "kevin"]
salary = [200, 400, 300, 500, 600, 300]
df = pd.dataframe(
{"name": employees, "joined date": pd.to_datetime(list_of_dates), "salary": salary}
)
df["status"] = np.where(df["salary"] >= 400, "senior", "junior")
print(df)
输出:
name joined date salary status
0 hisila 2019-11-20 200 junior
1 shristi 2020-01-02 400 senior
2 zeppy 2020-02-05 300 junior
3 alina 2020-03-10 500 senior
4 jerry 2020-04-16 600 senior
5 kevin 2020-05-01 300 junior
如果满足条件,则 np.where(condition, x, y)
返回 x,否则返回 y。
如果满足给定条件,上面的代码将在 df
中创建一个新列 status
,其值为 senior
。否则,将该值设置为初级。
np.select()
方法
np.where()将条件列表和选择列表作为输入,并根据条件返回从选择列表中的元素构建的数组。当我们有两个或多个条件时,可以使用此方法根据 pandas 中的给定条件创建 dataframe 列。
import pandas as pd
import numpy as np
list_of_dates = [
"2019-11-20",
"2020-01-02",
"2020-02-05",
"2020-03-10",
"2020-04-16",
"2020-05-01",
]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry", "kevin"]
salary = [200, 400, 300, 500, 600, 300]
df = pd.dataframe(
{"name": employees, "joined date": pd.to_datetime(list_of_dates), "salary": salary}
)
conditionlist = [
(df["salary"] >= 500),
(df["salary"] >= 300) & (df["salary"] < 300),
(df["salary"] <= 300),
]
choicelist = ["high", "mid", "low"]
df["salary_range"] = np.select(conditionlist, choicelist, default="not specified")
print(df)
输出:
name joined date salary salary_range
0 hisila 2019-11-20 200 low
1 shristi 2020-01-02 400 black
2 zeppy 2020-02-05 300 low
3 alina 2020-03-10 500 high
4 jerry 2020-04-16 600 high
5 kevin 2020-05-01 300 low
这里,如果满足条件列表中的第一个条件的行,则该特定行的列 salary_range
的值将被设置为选择列表中的第一个元素。条件列表中的其他条件类似。如果不满足条件列表中的任何条件,则将该行的 salary_range
列的值设置为 np.where()
方法中的默认参数的值,例如,not specified
。
pandas.dataframe.apply
根据 pandas 中的给定条件创建新的 dataframe 列
pandas.dataframe.apply 返回一个 dataframe
沿 dataframe 的给定轴应用给定函数的结果。
语法:
dataframe.apply(self, func, axis=0, raw=false, result_type=none, args=(), **kwds)
func
代表要应用的函数。
axis
代表应用该函数的轴。我们可以使用 axis=1
或 axis = 'columns'
将函数应用于每一行。
我们可以使用此方法检查条件并为新列的每一行设置值。
import pandas as pd
import numpy as np
list_of_dates = [
"2019-11-20",
"2020-01-02",
"2020-02-05",
"2020-03-10",
"2020-04-16",
"2020-05-01",
]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry", "kevin"]
salary = [200, 400, 300, 500, 600, 300]
df = pd.dataframe(
{"name": employees, "joined date": pd.to_datetime(list_of_dates), "salary": salary}
)
def set_values(row, value):
return value[row]
map_dictionary = {200: "low", 300: "low", 400: "mid", 500: "high", 600: "high"}
df["salary_range"] = df["salary"].apply(set_values, args=(map_dictionary,))
print(df)
输出:
name joined date salary salary_range
0 hisila 2019-11-20 200 low
1 shristi 2020-01-02 400 mid
2 zeppy 2020-02-05 300 low
3 alina 2020-03-10 500 high
4 jerry 2020-04-16 600 high
5 kevin 2020-05-01 300 low
在这里,我们定义了一个函数 set_values()
,该函数使用 df.apply()
应用于每一行。该函数根据该行的 salary
列的值来设置 salary_range
列的每一行的值。我们建立了一个 map_dictionary
来根据 salary
列中的数据来决定 salary_range
列的数值。当新列有很多选项时,此方法为我们提供了更大的灵活性。
pandas.series.map()
根据 pandas 中的给定条件创建新的 dataframe 列
我们也可以使用 pandas.series.map() 创建新的 dataframe
列基于 pandas 中的给定条件。该方法适用于系列的元素方式,并根据可能是字典,函数或系列的输入将值从一列映射到另一列。
import pandas as pd
import numpy as np
list_of_dates = [
"2019-11-20",
"2020-01-02",
"2020-02-05",
"2020-03-10",
"2020-04-16",
"2020-05-01",
]
employees = ["hisila", "shristi", "zeppy", "alina", "jerry", "kevin"]
salary = [200, 400, 300, 500, 600, 300]
df = pd.dataframe(
{"name": employees, "joined date": pd.to_datetime(list_of_dates), "salary": salary}
)
map_dictionary = {200: "low", 300: "low", 400: "mid", 500: "high", 600: "high"}
df["salary_range"] = df["salary"].map(map_dictionary)
print(df)
输出:
name joined date salary salary_range
0 hisila 2019-11-20 200 low
1 shristi 2020-01-02 400 mid
2 zeppy 2020-02-05 300 low
3 alina 2020-03-10 500 high
4 jerry 2020-04-16 600 high
5 kevin 2020-05-01 300 low
它创建一个新列 salary_range
,并根据 map_dictionary
中的键值对设置该列每一行的值。
转载请发邮件至 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 系列日期时间转换为字符串