[英]Transpose CSV into DF Pandas
I have a JSON that looks like this:我有一个看起来像这样的 JSON:
{ID:{"ATT1": "VAL", "ATT2": "VAL","ATT3": "VAL",...}}
This JSON has around 3000+ ID's inside of it, all with its own dict of ATT/VAL pairs.这个 JSON 里面有大约 3000 多个 ID,所有这些都有自己的 ATT/VAL 对字典。
After I read that I used this:在我阅读之后,我使用了这个:
with open("result.json", "r") as data_file:
data = json.load(data_file)
df = pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in data.items() ]))
df.to_csv("pandarised.csv", index=False)
Which then gives me a csv like this:然后给我一个像这样的 csv :
ID
{"ATT": "some att", "VAL": "some val"}
{"ATT": "some att", "VAL": "some val"}
{"ATT": "some att", "VAL": "some val"}
{"ATT": "some att", "VAL": "some val"}
{"ATT": "some att", "VAL": "some val"}
{"ATT": "some att", "VAL": "some val"}
There are about 3000 columns, where each column has a new ID and its own set of ATT/VAL dicts.大约有 3000 列,其中每一列都有一个新的 ID 和它自己的一组 ATT/VAL 字典。 I was trying to read this from DF into Pandas as a new DF, and was trying to make it look like this:我试图将这个从 DF 读入 Pandas 作为一个新的 DF,并试图让它看起来像这样:
ATT ATT2 ATT3 etc..
ID VAL VAL VAL etc..
ID VAL VAL VAL etc..
ID VAL VAL VAL etc..
ID VAL VAL VAL etc..
ID VAL VAL VAL etc..
etc..
I was thinking to try using either concat with some combination of transpose but I cant seem get a grip on where to start with this.我正在考虑尝试使用任何一个 concat 和一些转置组合,但我似乎无法掌握从哪里开始。
Thank you谢谢
I think this is what you are after:我认为这就是你所追求的:
data = [{"ID":{"ATT": "VAL", "ATT2": "VAL"}},
{"ID":{"ATT": "VAL", "ATT2": "VAL"}}]
df = pd.DataFrame()
for row in data:
df = df.append(row[list(row.keys())[0]], ignore_index=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.