Long Le
Long Le

Reputation: 114

CSV file append value from json value in a column

I have a CSV like this

SKU     NAME          VALUE
123     PRODUCT-1     {"Size": "16x7", "PCD": "5x114.3", "Offset": "48"}
456     PRODUCT-2     {"Drill": "29-15", "Face": "Face-4", "Rim Type": "L Center"}
789     PRODUCT-3     {"Bore": "73.1", "Drill": "32-13", "Face": "Face-3"}

I would like to use python3 pandas to create into:

SKU     NAME          SIZE     PCD     OFFSET     DRILL     FACE     etc...
123     PRODUCT-1     16x7     5x114.3 48
456     PRODUCT-2                                 29-15     Face-4    etc...               
789     PRODUCT-3                                 32-13     Face-3    etc...

I tried to use pd.read_json and tolist() but I dont know where to go from there. I really appreciate it if you can help me with this.

Upvotes: 1

Views: 35

Answers (1)

BENY
BENY

Reputation: 323326

You can read the csv as normal then we convert it after loaded the data

df = pd.read_csv('yourfile.csv')
import ast 
df['VALUE'] = df['VALUE'].apply(ast.literal_eval)
df = df.join(pd.DataFrame(df.pop('VALUE').tolist(), index=df.index))

Upvotes: 1

Related Questions