[英]Pandas: Check if row exists with certain values
I have a two dimensional (or more) pandas DataFrame like this:我有一个像这样的二维(或更多)pandas DataFrame:
>>> import pandas as pd
>>> df = pd.DataFrame([[0,1],[2,3],[4,5]], columns=['A', 'B'])
>>> df
A B
0 0 1
1 2 3
2 4 5
Now suppose I have a numpy array like np.array([2,3])
and want to check if there is any row in df
that matches with the contents of my array.现在假设我有一个 numpy 数组,如
np.array([2,3])
并想检查df
中是否有任何行与我的数组内容匹配。 Here the answer should obviously true but eg.这里的答案显然应该是正确的,但是例如。
np.array([1,2])
should return false as there is no row with both 1 in column A and 2 in column B. np.array([1,2])
应该返回 false,因为没有行同时包含 A 列中的 1 和 B 列中的 2。
Sure this is easy but don't see it right now.当然这很容易,但现在还看不到。
Turns out it is really easy, the following does the job here:事实证明这真的很容易,以下是这里的工作:
>>> ((df['A'] == 2) & (df['B'] == 3)).any()
True
>>> ((df['A'] == 1) & (df['B'] == 2)).any()
False
Maybe somebody comes up with a better solution which allows directly passing in the array and the list of columns to match.也许有人想出了一个更好的解决方案,它允许直接传入数组和要匹配的列列表。
Note that the parenthesis around df['A'] == 2
are not optional since the &
operator binds just as strong as the ==
operator.请注意,围绕
df['A'] == 2
的括号不是可选的,因为&
运算符的绑定与==
运算符一样强。
an easier way is:更简单的方法是:
a = np.array([2,3])
(df == a).all(1).any()
如果您还想返回发生匹配的索引:
index_list = df[(df['A'] == 2)&(df['B'] == 3)].index.tolist()
An answer that works with larger dataframes so you don't need to manually check for each columns:一个适用于较大数据框的答案,因此您无需手动检查每一列:
import pandas as pd
import numpy as np
#define variables
df = pd.DataFrame([[0,1],[2,3],[4,5]], columns=['A', 'B'])
a = np.array([2,3])
def check_if_np_array_is_in_df(df, a):
# transform a into a dataframe
da = pd.DataFrame(np.expand_dims(a,axis=0), columns=['A','B'])
# drop duplicates from df
ddf=df.drop_duplicates()
result = pd.concat([ddf,da]).shape[0] - pd.concat([ddf,da]).drop_duplicates().shape[0]
return result
print(check_if_np_array_is_in_df(df, a))
print(check_if_np_array_is_in_df(df, [1,3]))
To find rows where a single column equals a certain value:要查找单列等于某个值的行:
df[df['column name'] == value]
To find rows where multiple columns equal different values, Note the inner ():要查找多列等于不同值的行,请注意内部 ():
df[(df["Col1"] == Value1 & df["Col2"] == Value2 & ....)]
a simple solution with dictionary一个简单的字典解决方案
def check_existance(dict_of_values, df):
v = df.iloc[:, 0] == df.iloc[:, 0]
for key, value in dict_of_values.items():
v &= (df[key] == value)
return v.any()
import pandas as pd
df = pd.DataFrame([[0,1],[2,3],[4,5]], columns=['A', 'B'])
this_row_exists = {'A':2, 'B':3}
check_existance(this_row_exists, df)
# True
this_row_does_not_exist = {'A':2, 'B':5}
check_existance(this_row_does_not_exist, df)
# False
If you want to return the row where the matches occurred:如果要返回匹配发生的行:
resulting_row = df[(df['A'] == 2)&(df['B'] == 3)].values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.