0

I have csv file , which have one column and inside this column have string , string contains many values , i want to convert this string in muultiple columns

here is example data:

df = pd.DataFrame({'column1':[{'A':2,'B':3,'c':2}]})
print(df)
                    column1
0  {'A': 2, 'B': 3, 'c': 2}
1  {'A': 3, 'B': 5, 'c': 10}
i want output:

df = pd.DataFrame({'A':[2],'B':[3],'c':[2]})

i got this solution and it's work but this solution have * expression i am not sure this is about what. except this have any other effiecient solution ?

pd.DataFrame([*df['column1'].apply(eval)])
waqar ali
  • 79
  • 6
  • 2
    Does this answer your question? [Split / Explode a column of dictionaries into separate columns with pandas](https://stackoverflow.com/questions/38231591/split-explode-a-column-of-dictionaries-into-separate-columns-with-pandas) – mozway Jun 22 '22 at 05:17

2 Answers2

0

How about this

df = pd.DataFrame({'column1': [{'A': 2, 'B': 3, 'C': 2}, {'A': 3, 'B': 5, 'C': 10}]})
allkeys = set().union(*df['column1'])
pd.DataFrame.from_dict({k: [[d[k] for d in df['column1'] if k in d]] for k in allkeys})

output is

         C       B       A
0  [2, 10]  [3, 5]  [2, 3]
Lazyer
  • 917
  • 1
  • 6
  • 16
0

Answering your original question of "what does * expression mean", I believe this explanation would help.

PEP 448 proposes extended usages of the * iterable unpacking operator and ** dictionary unpacking operators to allow unpacking in more positions, an arbitrary number of times, and in additional circumstances. Specifically, in function calls, in comprehensions and generator expressions, and in displays.

The documentation

https://peps.python.org/pep-0448/