Super simple request but I'm frustrated by my attempts: I got a list of users, cancelled.csv, that cancelled orders and I would like to create a new column called "UNRELIABLE" in my transactions.csv and insert "True" or "False" (1,0 works too) if "user_id" matches in cancelled.csv. Thank you!
Asked
Active
Viewed 42 times
0
-
2https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column Check out the link – sophocles Feb 13 '21 at 14:04
-
1- merge the dataframes. 2- then you can create a new column by https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns-apply-a-function-o – r.burak Feb 13 '21 at 14:56
1 Answers
0
You could use 'isin' like in this code snippet:
import pandas as pd
transactions = pd.DataFrame({'id': [1,2,3,4,5,6], 'username': list('abcdef')})
cancelled = pd.DataFrame({'id': [2,3], 'username': list('bc')})
transactions['Unreliable'] = transactions['id'].isin(cancelled['id'])
transactions['Reliable'] = ~transactions['id'].isin(cancelled['id'])
transactions.head()

Peter Rüthemann
- 39
- 2