4
  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

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

Equivalent alternative: pd.DataFrame(series.values.tolist(), index=series.index)
Great! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.