获取pandas数据框中,两列组合的某个值大于或等于给定列表中的值的行。

huangapple go评论70阅读模式
英文:

Get rows of a pandas dataframe where one the values of a combination of two columns are greater than or equal to those given in a list

问题

我有以下的数据框:

df = pd.DataFrame({"a": [1,1,1,2,2,2,3,3,3,4,4,4], "b": [4,5,6,4,5,6,4,5,6,4,5,6]})

我收到了以下列表:

l = ["15", "24", "36", "45"]

我需要根据列表筛选df的行。例如,我需要所有列“a”中值为1且列“b”中值大于或等于5的行,列“a”中值为2且列“b”中值大于或等于4的行,列“a”中值为3且列“b”中值大于或等于6的行,依此类推。

因此,输出将如下所示。

英文:

I have the following dataframe:

df = pd.DataFrame({"a": [1,1,1,2,2,2,3,3,3,4,4,4], "b": [4,5,6,4,5,6,4,5,6,4,5,6]})

https://i.stack.imgur.com/SrG0e.png

I have been given the following list:

l = ["15", "24", "36", "45"]

I need to filter the rows of df based on the list. For example, I need all the rows where the value in column "a" is 1 and the values in columns "b" is greater than or equal to 5, the rows where the value in column "a" is 2 and the values in columns "b" is greater than or equal to 4, the rows where the value in column "a" is 3 and the values in columns "b" is greater than or equal to 6 and so on.

So, the output would look like,
https://i.stack.imgur.com/RJ7HA.png

I can use iteration but I believe there must be better way in pandas. Any help is greatly appreciated.

答案1

得分: -1

你可以使用映射 Series/dictionary 与 mapboolean indexing

s = pd.Series({int(x[0]): int(x[1]) for x in l})
# s = {int(x[0]): int(x[1]) for x in l}

out = df[df['b'].ge(df['a'].map(s))]

输出:

    a  b
1   1  5
2   1  6
3   2  4
4   2  5
5   2  6
8   3  6
10  4  5
11  4  6
英文:

You can use a mapping Series/dictionary with map and boolean indexing:

s = pd.Series({int(x[0]): int(x[1]) for x in l})
# s = {int(x[0]): int(x[1]) for x in l}

out = df[df['b'].ge(df['a'].map(s))]

Output:

    a  b
1   1  5
2   1  6
3   2  4
4   2  5
5   2  6
8   3  6
10  4  5
11  4  6

huangapple
  • 本文由 发表于 2023年4月17日 20:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定