df['overall_rating'] = df['rating'].apply(lambda x: x.get('overall'))
should give you the results
c = ['customer_id','rating']
d = [[44224,{'overall': 5, 'description': 3}],
[55243,{'overall': 3, 'description': 2}]]
import pandas as pd
df = pd.DataFrame(d,columns=c)
print (df)
df['overall_rating'] = df['rating'].apply(lambda x: x.get('overall'))
print (df)
Output of this is:
Original DataFrame:
customer_id rating
0 44224 {'overall': 5, 'description': 3}
1 55243 {'overall': 3, 'description': 2}
Updated DataFrame:
customer_id rating overall_rating
0 44224 {'overall': 5, 'description': 3} 5
1 55243 {'overall': 3, 'description': 2} 3
Or you can give:
df['overall_rating'] = pd.DataFrame([x for x in df['rating']])['overall']
The output of this will also be the same:
c = ['customer_id','rating']
d = [[44224,{'overall': 5, 'description': 3}],
[55243,{'overall': 3, 'description': 2}]]
import pandas as pd
df = pd.DataFrame(d,columns=c)
print (df)
df['overall_rating'] = pd.DataFrame([x for x in df['rating']])['overall']
#df['overall_rating'] = df['rating'].apply(lambda x: x.get('overall'))
print (df)
Original DataFrame:
customer_id rating
0 44224 {'overall': 5, 'description': 3}
1 55243 {'overall': 3, 'description': 2}
Updated DataFrame:
customer_id rating overall_rating
0 44224 {'overall': 5, 'description': 3} 5
1 55243 {'overall': 3, 'description': 2} 3
Example with dictionary having float value and dictionary without an entry for 'overall'
c = ['customer_id','rating']
d = [[44224,{'overall': 5, 'description': 3}],
[55243,{'overall': 3, 'description': 2}],
[11223,{'overall': 1.5, 'description': 2}],
[12345,{'description':3}]]
import pandas as pd
df = pd.DataFrame(d,columns=c)
print (df)
df['overall_rating'] = df['rating'].apply(lambda x: x.get('overall'))
print (df)
The output of this is:
Input DataFrame
customer_id rating
0 44224 {'overall': 5, 'description': 3}
1 55243 {'overall': 3, 'description': 2}
2 11223 {'overall': 1.5, 'description': 2}
3 12345 {'description': 3}
The Updated DataFrame is:
customer_id rating overall_rating
0 44224 {'overall': 5, 'description': 3} 5.0
1 55243 {'overall': 3, 'description': 2} 3.0
2 11223 {'overall': 1.5, 'description': 2} 1.5
3 12345 {'description': 3} NaN