0

I have data saved in a csv. I am querying this data using Python and turning it into a Pandas DataFrame. I have a column called team_members in my dataframe.It has a dictionaryof values. The column looks like so when called:

dt.team_members[1] 

Output:

"[{'name': 'LearnFromScratch', 'tier': 'novice tier'}, {'name': 'Scratch', 'tier': 'novice tier'}]" 

I tried to see this explanation and other similar:

Splitting multiple Dictionaries within a Pandas Column

But they do not work

I want to get a column called name with the names of the members of the team and another with tier of each member.

Can you help me?

Thanks!!

3
  • It's easy enough to use apply or map to turn this into some other data structure. The better question is how did you end up with this in the first place? Commented Jul 4, 2019 at 17:46
  • what is your expected output? Commented Jul 4, 2019 at 18:34
  • A column with names: LearnFromScratch, Scratch. A and other with tier. Commented Jul 4, 2019 at 21:05

3 Answers 3

1

I assume the output of dt.team_members[1] is a list.
If so, you can directly pass that list to create a dataframe something like:

pd.DataFrame(dt.team_members[1]) 
Sign up to request clarification or add additional context in comments.

Comments

1

You can extract name while looping over it

list(map(lambda x: x.get("name"), dt.team_members[1])) 

if you need a new dataframe: then follow @vivek answer:

pd.DataFrame(dt.team_members[1]) 

2 Comments

He wants to create a new dataframe not the list I believe.
exactly, i want a new df, with the dictionary values ​​in different columns.
0

You can try this-

 list = dt.team_member[1] li = [] for x in range(len(list)): t = {list[x]['name']:list[x]['tier']} li.append(t) print(li) 

Output is -

 [{'LearnFromScratch': 'novice tier'}, {'Scratch': 'novice tier'}] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.