我有一个pandas数据帧列表df_list
。在该列表中,有4个数据帧在列名和行数方面看起来相似。
一个示例数据帧,位于列表的索引0处:
df_list[0]
:
str_col vals1 vals2
hi [1,2,3,5] [0.4,0.4,0.5, 0.1]
hagha [1,3,3,5] [0.1,0.4,0.5, 0.1]
me [4,4,3,5] [0.9,0.4,0.5, 0.1]
i know [1,4,3,5] [0.1,0.7,0.9, 0.0]
在所有数据框中,str_col
列保持不变。它不会改变。只有vals1
和vals2
发生了变化。 vals1
和vals2
是列表。 str_col
是字符串。
我最终想要的是数据帧列表中的json:
hi
- list index 0:
- vals1
- vals2
- list index 1:
- vals1
- vals2
- list index 2:
- vals1
- vals2
- list index 3:
- vals1
- vals2
- list index 4:
- vals1
- vals2
hagha
- list index 0:
- vals1
- vals2
- list index 1:
- vals1
- vals2
- list index 2:
- vals1
- vals2
- list index 3:
- vals1
- vals2
- list index 4:
- vals1
- vals2
and so on
以下更好的版本:(请不要将vals1和vals2的值与上面的示例进行比较)。
{
"final_json": {
"hi": {
"0": {
"vals1": [
1,
2,
3,
5
],
"vals2": [
0.4,
0.4,
0.5,
0.1
]
},
"1": {
"vals1": [
8,
5,
5,
3
],
"vals2": [
1,
0,
0,
0
]
},
"2": {
"vals1": [
2,
3,
6,
7
],
"vals2": [
1,
0,
0,
0
]
},
"3": {
"vals1": [
3,
3,
5,
3
],
"vals2": [
1,
0,
0,
0
]
}
},
"hagha": {
"0": {
"vals1": [
6,
8,
9,
0
],
"vals2": [
0.76,
0.76,
0.36,
0.363
]
},
"1": {
"vals1": [
2,
3,
4,
6
],
"vals2": [
0.63,
0.36,
0.46,
0.6
]
},
"2": {
"vals1": [
3,
6,
5,
5
],
"vals2": [
0.4,
0.64,
0.46,
0.456
]
},
"3": {
"vals1": [
4,
6,
3,
2
],
"vals2": [
0.657,
0.675,
0.64,
0.6
]
}
.
.
.
.
.
]
}
}
}
}
目前,我循环遍历列表中的所有数据框,但速度太慢。我想知道是否存在更快的解决方案(可to_json
)?
答案 0 :(得分:2)
import pandas as pd
import json
import pprint
我认为您可以先concat
列出key
参数,以区分每个DataFrame
:
df = pd.concat(dfs, keys=range(len(dfs)))
#print (df)
然后移除第一级MultiIndex
,groupby
并创建dictionaries
,最后通过to_dict
将输出转换为dict
:
d = (df.reset_index(level=1, drop=True)
.groupby('str_col')['vals1','vals2']
.apply(lambda x: x.to_dict(orient='index'))
.to_dict()
)
#add start of json
d = {"final_json": d}
pprint.pprint(d)
{'final_json': {'hagha': {0: {'vals1': [1, 3, 3, 5],
'vals2': [0.1, 0.4, 0.5, 0.1]},
1: {'vals1': [10, 30, 3, 5],
'vals2': [0.17, 0.47, 0.57, 0.17]}},
'hi': {0: {'vals1': [1, 2, 3, 5],
'vals2': [0.4, 0.4, 0.5, 0.1]},
1: {'vals1': [10, 20, 30, 50],
'vals2': [0.48, 0.48, 0.58, 0.18]}},
'i know': {0: {'vals1': [1, 4, 3, 5],
'vals2': [0.1, 0.7, 0.9, 0.0]},
1: {'vals1': [1, 4, 3, 5],
'vals2': [0.1, 0.7, 0.9, 0.0]}},
'me': {0: {'vals1': [4, 4, 3, 5],
'vals2': [0.9, 0.4, 0.5, 0.1]},
1: {'vals1': [4, 4, 3, 5],
'vals2': [0.9, 0.4, 0.5, 0.1]}}}}
#convert to json
json = json.dumps(d)
<强>设置强>:
df1 = pd.DataFrame({'str_col': ['hi', 'hagha', 'me', 'i know'], 'vals1': [[1, 2, 3, 5], [1, 3, 3, 5], [4, 4, 3, 5], [1, 4, 3, 5]], 'vals2': [[0.4, 0.4, 0.5, 0.1], [0.1, 0.4, 0.5, 0.1], [0.9, 0.4, 0.5, 0.1], [0.1, 0.7, 0.9, 0.0]]})
print (df1)
# str_col vals1 vals2
#0 hi [1, 2, 3, 5] [0.4, 0.4, 0.5, 0.1]
#1 hagha [1, 3, 3, 5] [0.1, 0.4, 0.5, 0.1]
#2 me [4, 4, 3, 5] [0.9, 0.4, 0.5, 0.1]
#3 i know [1, 4, 3, 5] [0.1, 0.7, 0.9, 0.0]
df2 = pd.DataFrame({'str_col': ['hi', 'hagha', 'me', 'i know'], 'vals1': [[10, 20, 30, 50], [10, 30, 3, 5], [4, 4, 3, 5], [1, 4, 3, 5]], 'vals2': [[0.48, 0.48, 0.58, 0.18], [0.17, 0.47, 0.57, 0.17], [0.9, 0.4, 0.5, 0.1], [0.1, 0.7, 0.9, 0.0]]})
print (df2)
# str_col vals1 vals2
#0 hi [10, 20, 30, 50] [0.48, 0.48, 0.58, 0.18]
#1 hagha [10, 30, 3, 5] [0.17, 0.47, 0.57, 0.17]
#2 me [4, 4, 3, 5] [0.9, 0.4, 0.5, 0.1]
#3 i know [1, 4, 3, 5] [0.1, 0.7, 0.9, 0.0]
#create list of DataFrames
dfs = [df1, df2]