1

I have a Dataframe that is composed by 3760 rows. I want to split it in 10 parts of equal lenght and then use each new array as a column for a new DataFrame.

A way that I found to do this is:

alfa = np.array_split(dff, 10)
caa = pd.concat([alfa[0].reset_index(drop=True), alfa[1].reset_index(drop=True), alfa[2].reset_index(drop=True), alfa[3].reset_index(drop=True), 
           alfa[4].reset_index(drop=True), alfa[5].reset_index(drop=True), alfa[6].reset_index(drop=True), alfa[7].reset_index(drop=True), 
           alfa[8].reset_index(drop=True), alfa[9].reset_index(drop=True)], axis=1)

Not very cool, not very efficient.

Then I tried

teta = pd.concat(np.array_split(dff, 10), axis=1, ignore_index=True)

But it doesn't work as I wanted since it gives me this: enter image description here

I assume that is because the ignore_index works on the axis 1

Is there a better way to do it?

2 Answers 2

1

You can use list comprehension to concat your columns. This code expects your columns name is init_col:

chunks = 10
cols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
out = pd.concat(
    [np.array_split(dff, chunks)[i]
    .reset_index(drop=True)
    .rename(columns={"init_col": cols[i]})
    for i in range(chunks)], 
    axis=1
)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! Is there a way to assign columns name using this same line of code? @rachwa
How many columns has dff?
initially only 1, then 10
I edited my initial answer
0

It seems the original DataFrame seems to be just an array? In that case, perhaps you could use numpy.reshape:

new_df = pd.DataFrame(dff.to_numpy().reshape(10,-1).T, columns=dff.columns.tolist()*10)

Comments

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.