Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

python - Unable to extract the entire value of pandas cell

I have a csv that has values in a column like this enter image description here

When I read it in a df and print it, it looks like this: enter image description here

I understand that it is unable to pretty print it but when I try to extract value of x y width and height then on height it gives list index out of range

import pandas as pd
df = pd.read_csv("./data/SSCARDTemplate.csv")
for i in df["region_id"]:
    coor = df.loc[df["region_id"] == i, "region_shape_attributes"]
    dic = coor.to_string().split(",")
    print(coor.to_string())
    x = dic[1].split(":")[1]
    y = dic[2].split(":")[1]
    w = dic[3].split(":")[1]
    print(dic[4])
    h = dic[4].split(":")[1]
    break

This is reproducible pandas example

{'#filename': {0: 'SSCARDTemplate.jpg',
  1: 'SSCARDTemplate.jpg',
  2: 'SSCARDTemplate.jpg',
  3: 'SSCARDTemplate.jpg',
  4: 'SSCARDTemplate.jpg',
  5: 'SSCARDTemplate.jpg'},
 'file_size': {0: 110231,
  1: 110231,
  2: 110231,
  3: 110231,
  4: 110231,
  5: 110231},
 'file_attributes': {0: '{}', 1: '{}', 2: '{}', 3: '{}', 4: '{}', 5: '{}'},
 'region_count': {0: 6, 1: 6, 2: 6, 3: 6, 4: 6, 5: 6},
 'region_id': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5},
 'region_shape_attributes': {0: '{"name":"rect","x":402,"y":640,"width":521,"height":93}',
  1: '{"name":"rect","x":399,"y":723,"width":523,"height":88}',
  2: '{"name":"rect","x":402,"y":803,"width":526,"height":98}',
  3: '{"name":"rect","x":399,"y":889,"width":531,"height":101}',
  4: '{"name":"rect","x":404,"y":982,"width":526,"height":91}',
  5: '{"name":"rect","x":402,"y":1062,"width":529,"height":96}'},
 'region_attributes': {0: '{}', 1: '{}', 2: '{}', 3: '{}', 4: '{}', 5: '{}'}}
question from:https://stackoverflow.com/questions/65933348/unable-to-extract-the-entire-value-of-pandas-cell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can convert strings repr of dicts to dictionaries by json.loads, so if pass to DataFrame constructor get columns:

import json

df1 = pd.DataFrame([json.loads(x) for x in df['region_shape_attributes']])
print (df1)
   name    x     y  width  height
0  rect  402   640    521      93
1  rect  399   723    523      88
2  rect  402   803    526      98
3  rect  399   889    531     101
4  rect  404   982    526      91
5  rect  402  1062    529      96

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...