简体   繁体   中英

How to convert pandas series with numpy array values to dataframe

  1. I have a pandas series which values are numpy array. 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 :

       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:

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 .

Caveat. slide sub-index can have different number of values for different file values. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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