Reputation: 27
Hey guys not sure if what I am trying to do is possible but I want to make a new column in a dataframe based on whether the string says 'complete'. if it does i want the new column row to say 1 if not 0. Since I have many records i put it in a loop.
august_report['Lease'] = np.nan
for x in august_report.iterrows():
if august_report['Transaction Types'] =='Matched Lease transaction':
august_report['Lease'] = '1'
else:
august_report['Lease'] = '0'
Upvotes: 0
Views: 59
Reputation: 4618
can apply a lambda
df['Lease'] = df['Transaction Types'].apply(lambda x: '1' if x == 'Matched Lease transaction' else '0')
Upvotes: 0
Reputation: 476534
Yes, you can assign values to a column like:
august_report['Lease'] = august_report['Transaction Types'].eq('Matched Lease transaction').astype(int)
Probably a numerical 0
and 1
are better here (or even False
and True
, by dropping the .astype(int)
), since that shows that we are dealing with numerical data, not text data.
Upvotes: 0
Reputation: 733
Numpy
provides an easy way to do this if you have two values like your example (using np.where
). If you have multiple cases, look at np.select
.
import numpy as np
august_report['Lease'] = np.where(august_report['Transaction Types'] =='Matched Lease transaction', '1','0')
Upvotes: 1