2 回答
TA贡献1802条经验 获得超4个赞
展平内部字典并从字典列表中构造一个数据框:
d = [{d['name']:d['value'] for d in l} for l in s] # s being the pd.Series
df = pd.DataFrame(d)
快速检查 -
s = pd.Series([[{'name': 'svSum7Days', 'value': 0.0},
{'name': 'svSum91Days', 'value': 0.0},
{'name': 'svSum364Days', 'value': 423.0},
{'name': 'newPositionsCount60Days', 'value': 0.0}],
[{'name': 'svSum7Days', 'value': 1.0},
{'name': 'svSum91Days', 'value': 12.0},
{'name': 'svSum364Days', 'value': 424.0},
{'name': 'newPositionsCount60Days', 'value': 100.0}]])
d = [{d['name']:d['value'] for d in l} for l in s]
# [{'svSum7Days': 0.0, 'svSum91Days': 0.0, 'svSum364Days': 423.0,
# 'newPositionsCount60Days': 0.0}, {'svSum7Days': 1.0, ...
pd.DataFrame(d)
newPositionsCount60Days svSum364Days svSum7Days svSum91Days
0 0.0 423.0 0.0 0.0
1 100.0 424.0 1.0 12.0
TA贡献2003条经验 获得超2个赞
数据框可以从字典列表中创建。但是根据您的输出,您正在寻找转换后的数据框。
#your code goes here
import pandas as pd
#creating dummy series
data = pd.Series([{'name': 'svSum7Days', 'value': 0.0},
{'name': 'svSum91Days', 'value': 0.0},
{'name': 'svSum364Days', 'value': 423.0},
{'name': 'newPositionsCount60Days', 'value': 0.0}])
# Convert Series to list
data = data.tolist()
# Create a dataframe
df = pd.DataFrame(data)
# Transpose it
df = df.T
# Create column names
headers = df.iloc[0]
new_df = pd.DataFrame(df.values[1:], columns=list(headers))
print(new_df)
添加回答
举报