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
315 views
in Technique[技术] by (71.8m points)

How to extract value from Data dictionary like values in CSV file in Python Panda data frame

I have Header in CSV file with the data dictionary format like {"Id":"endDate","timeZone":"Z"} in column A , {"Id":"status"} in column B, {"Id":"ipAddress"} in column C in Panda Data Frame. How Can I show only Values in respective column in CSV file?

enter image description here

I tried to replace this code but seems not working.

df1.columns = df1.columns.str.replace(r'{"Id":"$', '')

I want to show only Values like in Column A > endDate, B > status, and C > ipAddress

enter image description here

Expected output enter image description here

question from:https://stackoverflow.com/questions/65713018/how-to-extract-value-from-data-dictionary-like-values-in-csv-file-in-python-pand

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

1 Reply

0 votes
by (71.8m points)

You've not actually provided the first row of your CSV which is the column names. I've reconstructed (also chose pipe separated). json.loads() to convert a string into a dict then extract key value you want as column name in a dict comprehension

import json
df1 = pd.read_csv(io.StringIO("""{"Id":"endDate","timeZone":"Z"}|{"Id":"status"}|{"Id":"ipAddress"}"""), sep="|")
df1 = df1.rename(columns={c:json.loads(c)["Id"] for c in df1.columns})
df1.columns

output

Index(['endDate', 'status', 'ipAddress'], dtype='object')

enhanced

  • do not rename columns that do not have Id key
  • deal with columns that are not a dict
import json
df1 = pd.read_csv(io.StringIO("""{"Id":"endDate","timeZone":"Z"}|{"Id":"status"}|{"Id":"ipAddress"}|{"NoId":"skip"}|notJSON"""), sep="|")
def decode(text):
    try:
        return json.loads(str(text))
    except ValueError:
        return {"Id":text}
df1 = df1.rename(columns={c:decode(c)["Id"] for c in df1.columns if "Id" in decode(c).keys()})
df1.columns

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

...