How to create multiple columns using the values of one in pandas?

Asked

Viewed 93 times

1

I have the following code:

df = pd.DataFrame(repeat_R)
print(df)

The dataframe I have is like this:

0 0 0.583822 1 0.582975 2 0.541191 3 0.526122 4 0.547239 5 0.555875 6 0.556838 7 0.524316 8 0.594906 9 0.559264 10 0.551321 11 0.530168 12 0.566200 13 0.499690 14 0.552580 15 0.523648 16 0.573826 17 0.554709 18 0.566220 19 0.505065 20 0.573585 21 0.586732 22 0.565730 23 0.534764 24 0.593547 25 0.573899 26 0.562164 27 0.529680 28 0.578324 29 0.484839 30 0.552086 31 0.549009 32 0.549631 33 0.499424 34 0.542479 35 0.511983 36 0.558355 37 0.491869 38 0.548544 39 0.485955

I want the new dataframe to have in the first column the first 10 values, in the second the 10 second values, in the third the 10 third values and in the fourth the 10 fourth values. I tried to divide the original dataframe into four dataframes and then join one, but not the right one:

df1 = df.iloc[0:9]
print(df1)
df2 = df.iloc[10:19]
print(df2)
df3 = df.iloc[20:29]
print(df3)
df4=df.iloc[30:39]
print(df4)
result = pd.concat([df1, df2, df3, df4], axis=1, join='inner')

Someone would know how to solve?

2 answers

2


I present below my solution. It is a particular implementation of a more general principle that can be used to implement a simpler solution. The principle is: create lists with the values of each column separately and then use the dataframe constructor pandas (pd.DataFrame) to create the new dataframe. Below:

import pandas as pd

def reshape(inf, sup, data):
    return [float(i) for i in data.iloc[inf:sup,:].values]

pd.DataFrame({'A': reshape(0,10, df) ,'B': reshape(10,20, df), 
              'C': reshape(20,30, df), 'D': reshape(30,40, df)}, index = range(10))

Upshot:

       A           B            C          D
0   0.583822    0.551321    0.573585    0.552086
1   0.582975    0.530168    0.586732    0.549009
2   0.541191    0.566200    0.565730    0.549631
3   0.526122    0.499690    0.534764    0.499424
4   0.547239    0.552580    0.593547    0.542479
5   0.555875    0.523648    0.573899    0.511983
6   0.556838    0.573826    0.562164    0.558355
7   0.524316    0.554709    0.529680    0.491869
8   0.594906    0.566220    0.578324    0.548544
9   0.559264    0.505065    0.484839    0.485955
  • Thanks Hartnäckig! It worked perfect, that’s just what I needed!

0

You can use the function reshape with the transpose numpy without the need to manually write a function or call it several times.

pd.DataFrame(df.values.reshape(-1, 10).T, columns=['A','B', 'C', 'D'])

#saida
    A           B           C           D
0   0.583822    0.551321    0.573585    0.552086
1   0.582975    0.530168    0.586732    0.549009
2   0.541191    0.566200    0.565730    0.549631
3   0.526122    0.499690    0.534764    0.499424
4   0.547239    0.552580    0.593547    0.542479
5   0.555875    0.523648    0.573899    0.511983
6   0.556838    0.573826    0.562164    0.558355
7   0.524316    0.554709    0.529680    0.491869
8   0.594906    0.566220    0.578324    0.548544
9   0.559264    0.505065    0.484839    0.485955

Browser other questions tagged

You are not signed in. Login or sign up in order to post.