如何将一系列元组转换为熊猫数据框?

时间:2018-11-20 22:32:14

标签: python pandas dataframe series

假设我们有以下pandas系列,这是归因于groupby之后在数据帧上应用了apply函数的结果。

OverridableJsonConverterDecorator

如果给出sigList = ['sig1','sig2','sig3'],我们可以将其转换为数据帧吗?

<class 'pandas.core.series.Series'>
0        (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2])
1     (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1])
2        (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4])
3        (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
4    (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6])
dtype: object

预先感谢

3 个答案:

答案 0 :(得分:4)

使用列表理解功能以老式(快速)的方式进行操作:

columns = ("Length Distance sig1Max sig2Max" 
           "sig3Max sig1Min sig2Min sig3Min").split()
df = pd.DataFrame([[a, b, *c, *d] for a,b,c,d in series.values], columns=columns)
print(df)
   Length  Distance  sig1Max  sig2Max  sig3Max  sig1Min  sig2Min  sig3Min
0       1         0      0.2      0.2      0.2      0.2      0.2      0.2
1       2      1000      0.6      0.7      0.5      0.1      0.3      0.1
2       1         0      0.4      0.4      0.4      0.4      0.4      0.4
3       1         0      0.5      0.5      0.5      0.5      0.5      0.5
4       3     14000      0.8      0.8      0.8      0.6      0.6      0.6

或者,也许您的意思是,它要更加动态一些

sigList = ['sig1', 'sig2', 'sig3']

columns = ['Length', 'Distance']
columns.extend(f'{s}{lbl}' for lbl in ('Max', 'Min') for s in sigList )

df = pd.DataFrame([[a,b,*c,*d] for a,b,c,d in series.values], columns=columns)
print(df)
   Length  Distance  sig1Max  sig2Max  sig3Max  sig1Min  sig2Min  sig3Min
0       1         0      0.2      0.2      0.2      0.2      0.2      0.2
1       2      1000      0.6      0.7      0.5      0.1      0.3      0.1
2       1         0      0.4      0.4      0.4      0.4      0.4      0.4
3       1         0      0.5      0.5      0.5      0.5      0.5      0.5
4       3     14000      0.8      0.8      0.8      0.6      0.6      0.6

答案 1 :(得分:1)

您可以检查

newdf=pd.DataFrame(s.tolist())
newdf=pd.concat([newdf[[0,1]],pd.DataFrame(newdf[2].tolist()),pd.DataFrame(newdf[3].tolist())],1)
newdf.columns = [
    "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
]
newdf
Out[163]: 
   Length  Distance  sig1Max   ...     sig1Min  sig2Min  sig3Min
0       1         0      0.2   ...         0.2      0.2      0.2
1       2      1000      0.6   ...         0.1      0.3      0.1
2       1         0      0.4   ...         0.4      0.4      0.4
3       1         0      0.5   ...         0.5      0.5      0.5
4       3     14000      0.8   ...         0.6      0.6      0.6
[5 rows x 8 columns]

答案 2 :(得分:0)

您可以展平每个元素,然后将每个元素转换为Series本身。将每个元素转换为一个Series会将主要Series(在以下示例中为load(); function load(){ var name=document.querySelector(".name"); var surname=document.querySelector(".surname"); $.getJSON("contact.json", function (data) { $.each(data, function (index, value) { console.log(value); name.innerHTML= value.name; surname.innerHTML= value.surname; }); }); )转换为DataFrame。然后只需设置所需的列名称即可。

例如:

s

然后您将import pandas as pd # load in your data s = pd.Series([ (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2]), (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1]), (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4]), (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]), (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6]), ]) def flatten(x): # note this is not very robust, but works for this case return [x[0], x[1], *x[2], *x[3]] df = s.apply(flatten).apply(pd.Series) df.columns = [ "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min" ] 设置为:

df