0

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

5 Answers5

2

This works:

df_final = pd.concat([df, df['shape_attributes'].apply(pd.Series)], axis = 1).drop('shape_attributes', axis = 1)
Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51
1

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

0

Python 3.x

Without dataframe

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:

With dataframe

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)
Community
  • 1
  • 1
ncica
  • 7,015
  • 1
  • 15
  • 37
0

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
cfort
  • 2,655
  • 1
  • 19
  • 29
0

You can apply pd.Series to the the column containing the dictionaries:

result = df['target_column'].apply(pd.Series)
kudeh
  • 883
  • 1
  • 5
  • 16