Shikha Mishra
Shikha Mishra

Reputation: 21

I have data in one column, how to extract that?

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

Answers (5)

ncica
ncica

Reputation: 7206

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)

Upvotes: 0

kudeh
kudeh

Reputation: 913

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

result = df['target_column'].apply(pd.Series)

Upvotes: 0

cfort
cfort

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

user8141219
user8141219

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

Rahul Agarwal
Rahul Agarwal

Reputation: 4100

This works:

df_final = pd.concat([df, df['shape_attributes'].apply(pd.Series)], axis = 1).drop('shape_attributes', axis = 1)

Upvotes: 2

Related Questions