[英]How to convert pandas series with numpy array values to dataframe
series = pd.Series([np.array([1,2,3,4]), np.array([5,6,7,8]), np.array([9,10,11,12])], index=['file1', 'file2', 'file3'])
file1 [1, 2, 3, 4]
file2 [5, 6, 7, 8]
file3 [9, 10, 11, 12]
How can I expand it to a dataframe of the form df_concatenated
: 如何将其扩展为
df_concatenated
形式的数据df_concatenated
:
0 1 2 3
file1 1 2 3 4
file2 5 6 7 8
file3 9 10 11 12
series
is obtained from a different dataframe of the form: series
是从以下形式的其他数据框中获得的: DataFrame: 数据帧:
0 1
file slide
file1 1 1 2
2 3 4
file2 1 5 6
2 7 8
file3 1 9 10
2 11 12
by grouping on 'file' index with concatenation of columns. 通过将“文件”索引与列的连接分组。
def concat_sublevel(data):
return np.concatenate(data.values)
series = data.groupby(level=[0]).apply(concat_sublevel)
May be somebody see a better way to come from dataframe data
to df_concatenated
. 可能有人看到将数据帧
data
为df_concatenated
的更好方法。
Caveat. 警告。
slide
sub-index can have different number of values for different file
values. 对于不同的
file
值, slide
子索引可以具有不同数量的值。 In such a case I need to repeat one of the rows to get the same dimensions in all resulting rows 在这种情况下,我需要重复一行,以在所有结果行中获得相同的尺寸
You can try of using pandas Dataframe from records 您可以尝试使用记录中的pandas Dataframe
pd.DataFrame.from_records(series.values,index=series.index)
Out: 日期:
0 1 2 3
file1 1 2 3 4
file2 5 6 7 8
file3 9 10 11 12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.