[英]How to do group by and take Count of one column divide by count of unique of second column of data frame in python pandas?
[英]Checking for unique values in Pandas Data frame column and crossreferenceing with a second column
我有一個 pandas dataframe 看起來像下面這樣。
我想檢查用戶 ID 中的值以查看它是否是唯一的。 如果是這樣,那么我想檢查許可證類型列以查看它是否是完整試用版,然后在新列“Full_direct”中返回 1。 否則,我會在“full_direct”列中返回 0。
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
我做了這個嘗試,但無法以這種方式遍歷 dataframe。 我希望看看是否有人可以提供建議。 謝謝!
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
我們這里不需要for循環,讓我們嘗試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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.