Reputation: 855
How can I extract a column as pandas df from a following pandas df containing dictionary: (I need all values of 'name' with the index)
nutrients_df
Out[63]:
nutrients
0 [{'code': '203cp1252', 'name': 'Proteincp1252'...
1 [{'code': '203cp1252', 'name': 'Proteincp1252'...
2 [{'code': '203cp1252', 'name': 'Proteincp1252'...
3 [{'code': '203cp1252', 'name': 'Proteincp1252'...
4 [{'code': '203cp1252', 'name': 'Proteincp1252'...
5 [{'code': '203cp1252', 'name': 'Proteincp1252'...
6 [{'code': '203cp1252', 'name': 'Proteincp1252'...
"nutrients_df" is defined as pandas df from json database as follows:
nutrient_name=[]
for index, row in data_df.iterrows():
nutrients1 = row['nutrients']
nutrients.append(nutrients1)
nutrients_df = pd.DataFrame({'nutrients': nutrients})
Upvotes: 0
Views: 704
Reputation: 164673
I'm not certain what data type is present in your df.nutrients
series. Below are a couple of examples of how you might extract name
from within a dictionary-like object.
import pandas as pd
from ast import literal_eval
# If your columns are genuine dictionaries
df = pd.DataFrame([[{'code': '203cp1252', 'name': 'Proteincp1252'}],
[{'code': '203cp1252', 'name': 'Proteincp1253'}],
[{'code': '203cp1252', 'name': 'Proteincp1254'}]],
columns=['nutrients'])
df['name'] = df['nutrients'].apply(lambda x: x['name'])
# If your column is a string
df = pd.DataFrame([["{'code': '203cp1252', 'name': 'Proteincp1252'}"],
["{'code': '203cp1252', 'name': 'Proteincp1253'}"],
["{'code': '203cp1252', 'name': 'Proteincp1254'}"]],
columns=['nutrients'])
df['name'] = df['nutrients'].apply(lambda x: literal_eval(x)['name'])
Upvotes: 1