英文:
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 与 map
和 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))]
输出:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论