dictionary and none within dataframe
Question:
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.
Answers:
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
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')
Using .str['second']
it will try to get the ‘second’ key from the dictionaries if available. If this is not available, it will return None
(or NaN
if for example an integer is included in the column). The final .fillna('NA')
is used to convert these values to 'NA'
.
Output:
category
0 EUR
1 NA
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.
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
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')
Using .str['second']
it will try to get the ‘second’ key from the dictionaries if available. If this is not available, it will return None
(or NaN
if for example an integer is included in the column). The final .fillna('NA')
is used to convert these values to 'NA'
.
Output:
category
0 EUR
1 NA