我有pandas.DataFrame
,df
:
Property Area dist
A 50 2
B 100 3
C 20 10
D 1 15
E 20 16
F 3 25
我希望最终的数据帧具有以下形式:
Property Area dist
A 50 2
C 20 10
F 3 25
即:我想省略每个都小于8的行。
答案 0 :(得分:1)
我相信此代码符合您的问题陈述。基本思想是收集要保留的dist
值集,然后将这些值应用于数据帧。
<强>代码:强>
# find the dist values to keep
to_keep = set()
min_value = None
min_dist = 8
for dist in sorted(df['dist']):
if min_value <= dist - min_dist:
min_value = dist
to_keep.add(dist)
# build a new data frame with just the keep values
new_df = df.query('dist in @to_keep')
print(new_df)
<强>产地:强>
Area dist
A 50 2
C 20 10
F 3 25
示例数据:
import numpy as np
import pandas as pd
props = np.array([
('Property', 'Area', 'dist'),
('A', 50, 2),
('B', 100, 3),
('C', 20, 10),
('D', 1, 15),
('E', 20, 16),
('F', 3, 25),
])
df = pd.DataFrame(data=props[1:, 1:],
index=props[1:, 0],
columns=props[0, 1:]).apply(pd.to_numeric)