2021年1月7日星期四

How to split a pandas column with a list of dicts into separate columns for each key

Here's what I mean. I have an entire column of targets that I want to analyze but it's formatted in a very inaccessible way for someone of my skill level.

This is from just 1 cell: [{"target": "NAge, "segment": "21 and older"}, {"target": "MinAge", "segment": "21"}, {"target": "Retargeting", "segment": "people who may be similar to their customers"}, {"target": "Region", "segment": "the United States"}]

and another: [{"target": "NAge, "segment": "18 and older"}, {"target": "Location Type", "segment": "HOME"}, {"target": "Interest", "segment": "Hispanic culture"}, {"target": "Interest", "segment": "Republican Party (United States)"}, {"target": "Location Granularity", "segment": "country"}, {"target": "Country", "segment": "the United States"}, {"target": "MinAge", "segment": 18}]

What I need to do is to separate every "target" item to become the column label with each of its corresponding "segment" to be a possible value within that column.

Or, is the solution to create a function to call each dictionary key within each row to count frequency?

Help?

This is what it's supposed to look like as the output:

0  21 and older     21  people who may be similar to their customers  the United States  ...                               NaN                  NaN                NaN    NaN  1  18 and older     18                                           NaN                NaN  ...  Republican Party (United States)              country  the United States    NaN  2  18 and older     18                                           NaN                NaN  ...                               NaN              country  the United States  women```    ```           NAge MinAge                                   Retargeting             Region  ...                          Interest Location Granularity            Country Gender  0  21 and older     21  people who may be similar to their customers  the United States  ...                               NaN                  NaN                NaN    NaN  1  18 and older     18                                           NaN                NaN  ...  Republican Party (United States)              country  the United States    NaN  2  18 and older     18                                           NaN                NaN  ...                               NaN              country  the United States  women```  
https://stackoverflow.com/questions/65621510/how-to-split-a-pandas-column-with-a-list-of-dicts-into-separate-columns-for-each January 08, 2021 at 07:42AM

没有评论:

发表评论