Python 检查数据帧列中的唯一值并与第二列交叉引用
我有一个熊猫数据框,看起来像下面这样 我想检查用户ID中的值,看看它是否唯一。如果是这样,那么我想检查License Type列,看看它是否是一个完整的试用版,然后在新的“full_direct”列中返回1。否则,我会在“full_direct”列中返回0Python 检查数据帧列中的唯一值并与第二列交叉引用,python,pandas,Python,Pandas,我有一个熊猫数据框,看起来像下面这样 我想检查用户ID中的值,看看它是否唯一。如果是这样,那么我想检查License Type列,看看它是否是一个完整的试用版,然后在新的“full_direct”列中返回1。否则,我会在“full_direct”列中返回0 Date **User ID** Product Name License Type Month 0 2017-01-01 10431046623214402832 90295d1
Date **User ID** Product Name License Type Month
0 2017-01-01 10431046623214402832 90295d194237 trial 2017-01
1 2017-07-09 246853380240772174 29125b243095 trial 2017-07
2 2017-07-07 13685844038024265672 47423e1485 trial 2017-07
3 2017-02-12 2475366081966194134 202400c85587 full 2017-02
4 2017-04-08 761179767639020420 168300g168004 full 2017-04
我做了这个尝试,但无法以这种方式遍历数据帧。我想看看是否有人能提供建议。谢谢
for values in main_df['User ID']:
if values.is_unique and main_df['License Type'] == 'full':
main_df['Full_Direct'] = 1
else:
main_df['Full_direct'] = 0
我们这里不需要循环,让我们尝试
duplicated
df['Full_direct'] = ((~df['User ID'].duplicated(keep=False)) & (df['License Type'] == 'full')).astype(int)
修正你的代码
for values in df.index:
if df['UserID'].isin([df.loc[values,'User ID']]).sum()==1 and df.loc[values,'License Type'] == 'full':
df.loc[values,'Full_direct'] = 1
else:
df.loc[values,'Full_direct'] = 0
有趣!然而,我得到了一个错误:“DataFrame”对象没有属性“Text”,但不知道如何解决这个问题?道歉,还在学习much@SebhatYidelwo更新~应该是用户ID而不是文本,相信我检查过了!再次感谢