1

I have created a csv file in python using writer. I called this csv file "data" which consists of one column with 40 rows. Then I import this csv file in a new python script and I create a dataframe. However, in my new data frame I want to split these forty data points into 4 columns with 10 rows each. What I have tried so far is the following:

    data=pandas.read_csv("location/data.csv", header=None)
    frame=pandas.DataFrame(data[:10])
    frame['second column']=data[10:20]

This creates a new column in the dataframe "frame" called "second column" but all the entries are NAN. It seems that python can't recognized these entries from the csv file. Any help would be very appreciated. Below is the code I used to create the csv file:

    Var1="data"
    with open(Var1,"ab") as output:
           writer=csv.writer(output, lineterminator='\n')
           for val in variable:
                writer.writerow([val])

With this I basically saved the values of the array "variable" in a separate csv file.

1 Answer 1

1

This is not the most elegant solution, but I think it does solve your problem.

import numpy as np
import pandas as pd

x = list(range(0, 40))
data = pd.DataFrame(x, columns=["A"])
arr = data.as_matrix()

data = pd.DataFrame(arr.reshape((10, 4), order="F"),
                    columns=['A', 'B', 'C', 'D'])

To change the ordering in the reshaped data simply change the the order parameter from "F" to "C".

Sign up to request clarification or add additional context in comments.

2 Comments

I assume arr is some sample data you're using to show how to do the reshape, in which case you can do the same with arr = np.asarray(range(0,40))[:,None], without the need to go list->Dataframe->numpy.array.
I was trying to replicate the starting point that OP was using, specifically, a dataframe named data. Thanks for this code though. I didn't realize that np.newaxis was just None.

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.