Python Forum
Check if a value is present in each group
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if a value is present in each group
#1
Hi,

I have a dataframe with two columns : an 'ID' column and a column 'V1' with different values between 1 and 3.
I'd like to mark each ID with 1 if value 3 is present for this group and 0 otherwise.

The attachment is more clear. I'd like to obtain the 'Result'.

Thank you by advance !

Attached Files

Thumbnail(s)
   
Reply
#2
What have you tried?
Reply
#3
I havn't tried much...

I 'd like to combine df.groupby('ID') and np.where(df['V1']==3, 1, 0) but I don't think it's possible...
Reply
#4
Any idea ?
Thank you by advance for your responses
Reply
#5
Caveat: I practically never use pandas, so my answer may not be too great.

I recently created some online polls to keep the students occupied. The csv file has for example 3 rows of Q1, 3 rows of Q2 and so on, similar to your 3 rows AAA, BBB.

So I adapted the poll csv, made 3 columns, id, Qnr, Result

Result contains nothing to start with, shows up in pandas as NaN.

If you search online, you can very quickly find answers for your particular problem. There is always more than 1 way to skin a cat!

import pandas as pd
csv_file = '/home/pedro/myPython/pandas/test1.csv'
df = pd.read_csv(csv_file)
# try with these 2 I can't see how to combine them into 1 line!
df.loc[df['Qnr'] == 'Q3', 'Result'] = 1
df.loc[df['Qnr'] != 'Q3', 'Result'] = 0

# or like this
df['Result'] = df['Qnr'].apply(lambda x: 1 if x == 'Q3' else 0)
Reply
#6
I tried two things :

1) df['Result'] = df.groupby(df['ID']).apply(lambda x: np.where(df['Var1']==3,0,1))
It keeps running, but i'm pretty sure it won't work

2) df['Result'] = df['Var1'].groupby(df['ID']).apply(lambda x: 1 if x == '3' else 0)
I get the error : ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I don't get why



Any idea how to sort one of these two ideas ?

Thank you by advance
Reply
#7
(May-16-2022, 01:01 AM)Pedroski55 Wrote: Caveat: I practically never use pandas, so my answer may not be too great.

I recently created some online polls to keep the students occupied. The csv file has for example 3 rows of Q1, 3 rows of Q2 and so on, similar to your 3 rows AAA, BBB.

So I adapted the poll csv, made 3 columns, id, Qnr, Result

Result contains nothing to start with, shows up in pandas as NaN.

If you search online, you can very quickly find answers for your particular problem. There is always more than 1 way to skin a cat!

import pandas as pd
csv_file = '/home/pedro/myPython/pandas/test1.csv'
df = pd.read_csv(csv_file)
# try with these 2 I can't see how to combine them into 1 line!
df.loc[df['Qnr'] == 'Q3', 'Result'] = 1
df.loc[df['Qnr'] != 'Q3', 'Result'] = 0

# or like this
df['Result'] = df['Qnr'].apply(lambda x: 1 if x == 'Q3' else 0)

It doesn't wok because the "result" column is not grouped by ID
Reply
#8
Haha, what a wonderful thing the Internet is: instant answers

I could never have got to this on my own!

df['Result'] = df['Type'].isin(df.loc[df['Var1'].eq(3), 'Type']).astype(int)
df['Result'] = np.where(df['Type'].isin(df.loc[df['Var1'].eq(3), 'Type']), 1, 0)
df['Result'] = df['Var1'].eq(3).groupby(df['Type']).transform('any').astype(int)
Reply
#9
(May-16-2022, 10:24 AM)Pedroski55 Wrote: Haha, what a wonderful thing the Internet is: instant answers

I could never have got to this on my own!

df['Result'] = df['Type'].isin(df.loc[df['Var1'].eq(3), 'Type']).astype(int)
df['Result'] = np.where(df['Type'].isin(df.loc[df['Var1'].eq(3), 'Type']), 1, 0)
df['Result'] = df['Var1'].eq(3).groupby(df['Type']).transform('any').astype(int)


Thank you so much, it works !! :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Anaconda pip install mpyc Error pip-script.py is not present Anldra12 2 7,853 Dec-13-2021, 06:59 PM
Last Post: Anldra12
  No Scripts File present after python installation ag2207 5 4,909 Jul-30-2020, 11:11 AM
Last Post: buran
  Python function executes twice when region loop is present bluethundr 2 2,631 Jan-07-2020, 03:01 PM
Last Post: bluethundr
  "usr" directory not present and needed for cmd commands? ajetrumpet 2 2,152 Dec-19-2019, 05:59 PM
Last Post: ajetrumpet
  Deleting files not present in CSV spw2515 1 1,930 Sep-04-2019, 06:06 PM
Last Post: ichabod801
  Comparing the count of rows from the tables that are present in two different databas krt5 6 4,001 Feb-15-2019, 03:20 PM
Last Post: krt5
  check log filename with present date CyberDaemon 5 3,868 Aug-22-2018, 08:42 PM
Last Post: CyberDaemon
  How to find the list of dependencies not present in the remote artifactory repository csplrj 6 4,717 Mar-30-2018, 03:43 PM
Last Post: csplrj
  Need Help: Sort of Binary numbers based on 1's present abinashj 5 5,088 Jul-25-2017, 12:23 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020