python - 比较并查找 pandas 系列中缺失的字符串
问题描述
通过拥有以下两个熊猫系列,如何发现 df2 缺少'c'?或者索引 2 上存在缺失值。
df1 = pd.Series({'col1': ['a', 'b', 'c', 'd']})
df2 = pd.Series({'col2': ['a', 'b', 'd']})
可能会更容易的事情:我知道 df1 具有 df2 具有的所有值,并且我知道 df2 缺少多少值。
解决方案
您可以在系列中使用numpy.setdiff1d ,.values
如下所示:
import pandas as pd
import numpy as np
df1 = pd.Series({'col1': ['a', 'b', 'c', 'd']})
df2 = pd.Series({'col2': ['a', 'b', 'd']})
print(np.setdiff1d(df1['col1'], df2['col2']))
输出:
['c']
推荐阅读
- python - Python argparse 比较输入和默认文件名和类型是否相同?
- python - 如何通过比较每一行来找到二维数组中的不同数字?
- sql - 如何在 CosmosDB 中对字典键进行不区分大小写的搜索?
- ubuntu - 无法在 ubuntu 上安装 pgadmin4
- python - 返回字典值时第 4 行实际发生了什么
- r - 在同一个图中组合多个图,但使用 ggplot2 仅显示特定轴
- bash - 如何在 linux 中传递 golang 变量(可以包含任何字符)来回显二进制文件?
- dataweave - Dataweave 2.0 将 JSON 转换为 XML 的函数
- java - Java 8 按唯一名称过滤对象列表,同时只保留最高 ID?
- linux - 如何在子字符串列上grep