Reputation: 21
Ihave data in one column, how to extract that?
For example: Shape_attribute is col name {"name":"circle","cx":371,"cy":2921,"r":73} {"name":"circle","cx":3712,"cy":29212,"r":73} {"name":"circle","cx":371,"cy":2921,"r":73}
I want output as follows:
name cx cy r
circle 371 2921 73
circle 3712 29212 73
circle 371 2921 73
Note: all 4 values are in same column shape_attributes
Upvotes: 0
Views: 82
Reputation: 7206
Python 3.x
dict = {"name":"circle","cx":371,"cy":2921,"r":73}
keys= []
values = []
for key, value in dict.items():
keys.append(str(key))
values.append(str(value))
data = [keys, values]
for row in data: # Align right
print("{: >10} {: >10} {: >10} {: >10}".format(*row))
for row in data: # Align left
print("{: <10} {: <10} {: <10} {: <10}".format(*row))
or:
import pandas as pd
data={"name":"circle","cx":371,"cy":2921,"r":73}
df = pd.DataFrame([data], columns=data.keys())
#Remove index
result = df.to_string(index=False)
print(result)
Upvotes: 0
Reputation: 913
You can apply pd.Series to the the column containing the dictionaries:
result = df['target_column'].apply(pd.Series)
Upvotes: 0
Reputation: 2776
Make a Series from the dict, convert to a DataFrame and then Transpose:
d = {"name":"circle","cx":371,"cy":2921,"r":73}
pd.Series(d).to_frame().T
Output:
name cx cy r
0 circle 371 2921 73
Upvotes: 0
Reputation:
First create a dictionary of your values and then pass them to your Pandas Dataframe like this:
import pandas as pd
my_dict={"name":"circle","cx":371,"cy":2921,"r":73}
df=pd.DataFrame([my_dict],columns=my_dict.keys())
Also to learn more about converting dictionary to dataframe, visit this Dictionary to DataFrame
Upvotes: 1
Reputation: 4100
This works:
df_final = pd.concat([df, df['shape_attributes'].apply(pd.Series)], axis = 1).drop('shape_attributes', axis = 1)
Upvotes: 2