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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…