Reputation: 1355
I have a dataframe 'df'. The columns in the dataframe are of the type below -
ID float64
TYPE object
COMB_ID float64
AMT_DR float64
AMT_CR float64
The data in my dataframe looks like -
ID TYPE COMB_ID AMT_DR AMT_CR
0 3619998 E 84623 1169456 NaN
1 3619998 E 84624 315792 NaN
2 3619998 E 84625 73389 NaN
3 3619998 E 85947 NaN 1558637
4 3619999 E 84623 1488992 NaN
Now, I want to create a new column called 'Status' where in if AMT_DR is not null, then it should create a field like COMB_ID + 'Dr:' & same in case AMT_CR is not null. My pandas logic is as below -
df['Status'] = np.where(df['AMT_DR'].notnull(), df['COMB_ID'].astype(str) + '-Dr:', df["COMB_ID"].astype(str) + '-Cr:')
Now df looks like -
ID TYPE COMB_ID AMT_DR AMT_CR Status
0 3619998 E 84623 1169456 NaN 84623.0-Dr:
1 3619998 E 84624 315792 NaN 84624.0-Dr:
2 3619998 E 84625 73389 NaN 84625.0-Dr:
3 3619998 E 85947 NaN 1558637 85947.0-Cr:
4 3619999 E 84623 1488992 NaN 84623.0-Dr:
So, I dont want the ".0" part in the Status column. Can you please help with the same.
Thanks.
Upvotes: 0
Views: 5376
Reputation: 10060
You can slice the string like:
df["COMB_ID"].astype(str[:-1]) + '-Cr:'
or perhaps convert it to an integer.
EDIT: spell check
Upvotes: 2