简体   繁体   English

将一个 dataframe 中的一列与另一个单个值进行比较

[英]Compare a column in one dataframe with another single value

I have two dataframes:我有两个数据框:

data = {'a':['a','b','c','d','e','f','g'],
    'b':['Y','N','Y','Y','Y','N','Y']}
df = pd.DataFrame(data)

data1 = ['N']
df1 = pd.DataFrame(data1, columns=['Y/N'])

I would like to compare all elements in df with the single element in df2, so that the resulting dataframe is:我想将 df 中的所有元素与 df2 中的单个元素进行比较,结果 dataframe 是:

    a   b
0   b   N
1   f   N

How could I make it happen?我怎样才能让它发生?

Since you only have one value on your second dataframe you can do this like so:由于您的第二个 dataframe 只有一个值,您可以这样做:

df[df["b"] == df1["Y/N"].iloc[0]]

Also you don't need a dataframe with only one string if all you want to do is get the rows with values "N" , you can do this by simply:此外,如果您只想获取值为"N"的行,则不需要只有一个字符串的 dataframe ,您可以通过简单地执行此操作:

df[df["b"] == "N"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 将一列中列表中的项目与另一列中的单个值进行比较 - Pandas compare items in list in one column with single value in another column 如何比较另一列数据框中的一个可用列值是否可用以及如何提取第二个数据帧中的另一列(如果存在) - How to compare one column value available or not in another column dataframe and extract another column of second dataframe if present 如何使用 fuzzywuzzy 比率将一个数据框中的值与另一个数据框中的列进行比较 - How to compare a value in one dataframe to a column in another using fuzzywuzzy ratio 比较另一列 dataframe 中一列的值 dataframe - Compare values of one column of dataframe in another dataframe 遍历一个数据框中的单个列与另一个数据框中的列进行比较使用熊猫在第一个数据框中创建新列 - loop through a single column in one dataframe compare to a column in another dataframe create new column in first dataframe using pandas 将一个 dataframe 的两列与另一个 dataframe 的一列进行比较 - Compare two columns of one dataframe to one column of another dataframe Dataframe:比较列值和下一行 - Dataframe: compare column value and one row below 使用 Pandas 数据框将一列值与另一列中的其他元素列表进行比较 - using pandas dataframe compare one column value with other list of elements in another column 将一个 dataframe 中的一列与另一个 dataframe pandas 中的许多列进行比较 - Compare a column in one dataframe with many columns in another dataframe pandas 将列值与 dataframe 中的另一个值进行比较(天气预报) - compare column value with another value in a dataframe (weather data forecast)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM