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)])
CodePudding user response:
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]
CodePudding user response:
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