Reputation: 387
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.
Upvotes: 1
Views: 641
Reputation: 120
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"
.
Upvotes: 1