Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check values from list in DataFrame column and insert value by condition?

for example, I have the next list:

l = ['a', 'x', 't']

and the DataFrame:

a = [{'sufix': 'a', 'qty': 5}, {'sufix': 'b', 'qty': 2}, {'sufix': 'c', 'qty': 7}, {'sufix': 'x', 'qty': 9}, {'sufix': 't', 'qty': 4}, {'sufix': 'p', 'qty': 1}]
df = pd.DataFrame(a)
print(df)

What I need, if values from list -> l are in column df['sufix'] -> create new column df['yes'] and put value from column df['qty'], else df['yes'] = 0

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need the next result:

enter image description here

>Solution :

If I understand you correctly, probably something like this:

df['yes'] = df['qty'][df['sufix'].isin(l)]
df['yes'] = df['yes'].fillna(0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading