Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 415
Olga Gorun Avatar asked Oct 17 '25 21:10

Olga Gorun


1 Answers

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
like image 169
Naga kiran Avatar answered Oct 20 '25 10:10

Naga kiran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!