python-3.x - 在 pandas 中选择一个 df1 而不是 df2 中的行
问题描述
我有一个 df ,如下所示。
df1:
ID Age_days N_30 N_31_90 N_91_180
1 201 60 15 30
2 20 0 15 5
3 800 0 0 10
4 100 0 0 0
5 600 0 6 5
6 800 0 0 15
7 500 10 10 30
df2:
ID Age_days N_30 N_31_90 N_91_180
1 201 60 15 30
2 20 0 15 5
4 100 0 0 0
6 800 0 0 15
其中 ID 是 df1 和 df2 的主键。
我想从中选择 df1 中而不是 df2 中的行。
预期输出:
ID Age_days N_30 N_31_90 N_91_180
3 800 0 0 10
5 600 0 6 5
7 500 10 10 30
我尝试了以下代码:
df3 = df1[~df1.isin(df2)].dropna()
我在下面提到了松弛问题。 pandas 获取不在其他数据框中的行
common = df1.merge(df2,on=['col1','col2'])
print(common)
df1[(~df1.col1.isin(common.col1))&(~df1.col2.isin(common.col2))]
我的疑问是 col1 和 col2 在我的情况下应该是什么。
解决方案
推荐阅读
- javascript - 两个 div 合二为一不处理鼠标事件
- scala - 隐式 Map 使 Char 方法无法编译
- javascript - 如何在 Jest 的函数组件中测试回调函数
- google-bigquery - 设置 BigQuery 计划查询时,如何避免“创建计划查询时出错:调用者没有权限”?
- logstash - 找不到 Logstash JDBC 类
- c++ - c ++ lambda已使用但未初始化
- ubuntu - `/sys/fs/cgroup/pids/user.slice/user-1000.slice/pids.max`在重新连接到机器(ssh)后改变
- python - 将复杂条件分解为子条件 - Python
- javascript - 在 UI 上解析上传的 CSV 文件(约 8GB)时将非常大的 JSON 对象存储在内存中
- python - 在snakemake中缺少所有规则的输入文件