如何根据pandas中某些列中的值从DataFrame中选择行?
在SQL中我会使用:
SELECT *
FROM table
WHERE colume_name = some_value
Run Code Online (Sandbox Code Playgroud)
我试着看看熊猫文档,但没有立即找到答案.
对于每对src
和dest
机场城市,我想在a
给定列值的情况下返回列的百分位数b
.
我可以手动执行此操作:
示例df只有2对src/dest(我的实际df中有数千个):
dt src dest a b
0 2016-01-01 YYZ SFO 548.12 279.28
1 2016-01-01 DFW PDX 111.35 -65.50
2 2016-02-01 YYZ SFO 64.84 342.35
3 2016-02-01 DFW PDX 63.81 61.64
4 2016-03-01 YYZ SFO 614.29 262.83
{'a': {0: 548.12,
1: 111.34999999999999,
2: 64.840000000000003,
3: 63.810000000000002,
4: 614.28999999999996,
5: -207.49000000000001,
6: 151.31999999999999,
7: -56.43,
8: 611.37,
9: -296.62,
10: 6417.5699999999997,
11: -376.25999999999999,
12: 465.12,
13: -821.73000000000002,
14: 1270.6700000000001,
15: -1410.0899999999999,
16: …
Run Code Online (Sandbox Code Playgroud) 这个问题与这两个问题another和thisone非常相关,我什至会使用这个问题上非常有用的公认解决方案中的示例。这是已接受的解决方案中的示例(归功于 unutbu):
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
'B': 'one one two three two two one three'.split(),
'C': np.arange(8), 'D': np.arange(8) * 2})
print(df)
# A B C D
# 0 foo one 0 0
# 1 bar one 1 2
# 2 foo two 2 4
# 3 bar three 3 6
# 4 foo two 4 8
# 5 bar …
Run Code Online (Sandbox Code Playgroud)