简体   繁体   English

如何将具有numpy数组值的熊猫系列转换为数据框

[英]How to convert pandas series with numpy array values to dataframe

  1. I have a pandas series which values are numpy array. 我有一个熊猫系列,其值是numpy数组。 For simplicity, say 为了简单起见,说
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
  1. A wider version of the same problem. 相同问题的更广泛版本。 Actually the 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 . 可能有人看到将数据帧datadf_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM