[英]How can I convert a structure text file into a pandas dataframe
I have a text file with the following format:我有一个具有以下格式的文本文件:
"Model_1": {
"10000": {
"KPI_1": [
0,
3.0,
6.0
],
"KPI_2": [
0,
3.0,
6.0
],
}
...
"15000": {
"KPI_3": [
0,
3.0,
6.0
],
"KPI_4": [
0,
3.0,
6.0
],
...
}
"20000": {
"KPI_5": [
0,
3.0,
6.0
],
"KPI_6": [
0,
3.0,
6.0
],
...
}
}
"Model_2": {
"10000": {
"KPI_7": [
v1,
v2,
v3
],
"KPI_8": [
v1,
v2,
v3
],
...
}
"15000": {
"KPI_9": [
v1,
v2,
v3
],
"KPI_10": [
v1,
v2,
v3
],
...
}
"20000": {
"KPI_11": [
v1,
v2,
v3
],
"KPI_12": [
v1,
v2,
v3
],
...
}
}
And I would like to get a pandas df with the following structure:我想得到一个具有以下结构的熊猫 df:
Model_1 Model_2 ... Model_N
10000 15000 20000 10000 15000 20000 10000 15000 20000
KPI_1 {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3}
KPI_2 {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3}
KPI_3 {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3}
...
KPI_N {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3} {v1,v2,v3}
The data from the text file that you show us look much like a dictionary, only comma's seem missing at two places where I put the comment # I added a comma here :您向我们展示的文本文件中的数据看起来很像字典,只有在我放置注释的两个地方似乎缺少逗号# 我在这里添加了一个逗号:
Model_1: {
"10000": {
"KPI_1": [
0,
3.0,
6.0
],
"KPI_2": [
0,
3.0,
6.0
],
}, # I added a comma here
"15000": {
"KPI_3": [
0,
3.0,
6.0
],
"KPI_4": [
0,
3.0,
6.0
],
}, # I added a comma here
"20000": {
"KPI_5": [
0,
3.0,
6.0
],
"KPI_6": [
0,
3.0,
6.0
],
}
}
If it really are dictionaries, you can convert them to DataFrames:如果它真的是字典,你可以将它们转换为 DataFrames:
pd.DataFrame.from_dict(Model_1, orient='index')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.