python - Pandas 在基于其他列的列中删除值

标签 python pandas delete-row

有没有办法在 df 中删除单个条目?

df1:
ID  Count1  Value1  Count2  Value2
111 5   10  5   25
112 4   15  6   25
113 6   7   4   10
114 3   8   2   1
115 7   1   8   10

如果 Count1 < 5,我想删除任何 Value1,如果 Count2 < 5,我想删除任何 Value2。结果将是这样的:

ID  Count1  Value1  Count2  Value2
111 5   10  5   25
112 NA  NA  6   25
113 6   7   4   NA
114 NA  NA  NA  NA
115 7   1   8   10

谢谢!

最佳答案

使用位置

df.loc[df['Count1'] < 5, ['Count1','Value1']] = np.nan
df.loc[df['Count2'] < 5, ['Count2','Value2']] = np.nan

    ID  Count1  Value1  Count2  Value2
0   111 5.0     10.0    5.0     25.0
1   112 NaN     NaN     6.0     25.0
2   113 6.0     7.0     NaN     NaN
3   114 NaN     NaN     NaN     NaN
4   115 7.0     1.0     8.0     10.0

关于python - Pandas 在基于其他列的列中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47720990/

相关文章:

python - 什么时候会在 dict 上使用键值对作为 dict.update 方法?

python - 在 Pandas 和 pyodbc SQL Server 中使用百分比 (%) 没有错误

python - pandas read_sql 异常慢

python - 转换为 html 表时删除 pandas 数据框中的索引

iOS/objective-C : Swipe to Left causing crash in tableview created with code

python - PyPy 文件附加模式

python - 如何使用 Pandas 删除基于特定列的重复值?

在表格 View 单元格之间快速滑动?

python - 为什么 PYTHON 中的 SQL 查询中的删除不会提交数据库中的更改?

python - 您如何以与在 Django 中排序 QuerySets 相同的方式对列表进行排序?