Python 如果>;1其他列的唯一值

Python 如果>;1其他列的唯一值,python,python-3.x,pandas,Python,Python 3.x,Pandas,给定以下数据帧: import pandas as pd df = pd.DataFrame( {'A':['A','A','B','B','C','C'], 'B':['Y','Y','N','N','Y','N'], }) df A B 0 A Y 1 A Y 2 B N 3 B N 4 C Y 5 C N 我需要一行代码: 1.确定A的每个类别在B列中是否有超过1个唯一值(即A

给定以下数据帧:

import pandas as pd
df = pd.DataFrame(
        {'A':['A','A','B','B','C','C'],
         'B':['Y','Y','N','N','Y','N'],
        })
df

    A   B
0   A   Y
1   A   Y
2   B   N
3   B   N
4   C   Y
5   C   N
我需要一行代码: 1.确定A的每个类别在B列中是否有超过1个唯一值(即A列中的“C”类在B列中有2个唯一值,而A列中的“A”和“B”类各只有1个唯一值)。 2.仅当每个类别有超过1个唯一值时,才将B列中的值更改为“Y”(即B列中A列中类别“C”的两行都应有“Y”)

以下是期望的结果:

    A   B

0   A   Y
1   A   Y
2   B   N
3   B   N
4   C   Y
5   C   Y
提前感谢!

这应该可以:

import pandas as pd
df = pd.DataFrame(
        {'A':['A','A','B','B','C','C'],
         'B':['Y','Y','N','N','Y','N'],
        })

# Get unique items in each column A group
group_counts = df.groupby('A').B.apply(lambda x: len(x.unique()))
# Find all of them with more than 1 unique value
cols_to_impute = group_counts[group_counts > 1].index.values
# Change column B to 'Y' for such columns
df.loc[df.A.isin(cols_to_impute),'B'] = 'Y'

In [20]: df
Out[20]:
   A  B
0  A  Y
1  A  Y
2  B  N
3  B  N
4  C  Y
5  C  Y
这应该起作用:

import pandas as pd
df = pd.DataFrame(
        {'A':['A','A','B','B','C','C'],
         'B':['Y','Y','N','N','Y','N'],
        })

# Get unique items in each column A group
group_counts = df.groupby('A').B.apply(lambda x: len(x.unique()))
# Find all of them with more than 1 unique value
cols_to_impute = group_counts[group_counts > 1].index.values
# Change column B to 'Y' for such columns
df.loc[df.A.isin(cols_to_impute),'B'] = 'Y'

In [20]: df
Out[20]:
   A  B
0  A  Y
1  A  Y
2  B  N
3  B  N
4  C  Y
5  C  Y
你可以:

df['B'] = df.groupby('A')['B'].transform(lambda x: 'Y' if x.nunique() > 1 else x)
要获得:

   A  B
0  A  Y
1  A  Y
2  B  N
3  B  N
4  C  Y
5  C  Y
你可以:

df['B'] = df.groupby('A')['B'].transform(lambda x: 'Y' if x.nunique() > 1 else x)
要获得:

   A  B
0  A  Y
1  A  Y
2  B  N
3  B  N
4  C  Y
5  C  Y

或者,如果B列包含a列中给定类别的“Y”和“N”,则将B列中该类别的所有值更改为“Y”。听起来类似于“超过1个唯一值”?或者,如果B列包含a列中给定类别的“Y”和“N”,则将B列中该类别的所有值更改为“Y”听起来类似于“不止一个唯一值”?很好地使用了条件转换,比我的更简洁。做得好。很好地使用了条件转换,比我的更简洁。做得好。