I have the following dataframe:
I would like to modify the column category by implementing the following method : if the dictionary = None return "NA" (str) else I would like to keep only the value of 'second' (for example EUR in the first line) as a value for the column. Also if the 'second' is None so return "NA".
I tried the following logic : df['category']= df['category'].apply(lambda x: x['second'] if x is not None)
but it didn't work as there are some None.
CodePudding user response:
I think this will work:
import pandas as pd
df = pd.DataFrame(
{'category': [{'first': 'ABC',
'second': 'EUR'},
{'first': 'ABC',
'second': None}]})
df['category'] = df['category'].str['second'].fillna('NA')
Output:
category
0 EUR
1 NA
CodePudding user response:
You just have to add 'NA' if x is None else
to your lambda expression to check for None
first:
df['category'] = df['category'].apply(lambda x: 'NA' if x is None else #check for None
x['second'] if x['second'] is not None else 'NA') #check for second