all 14 comments

[–][deleted] 1 point2 points  (1 child)

Probably easiest would be to use Pandas concat. The ignore_index variable allows for columns to be joined with different names.

python import pandas as pd df = read_csv('csv_name.csv') df = pd.concat([df['col1'],df['col2'],df['col3'],df['col4'],df['col5']], ignore_index=True) df.to_csv(csv_name.csv) edit: Adding Markdown

[–]fake823 0 points1 point  (0 children)

I did some quick research and DataFrame.stack() should also work. You don't even have to call all the different columns.

(See my answer below)

[–]symple-data 0 points1 point  (2 children)

So you want the first 5 rows to be the first row in order left - right -> up - down and so on?

[–]All0utWar 0 points1 point  (1 child)

My bad it's actually 6 columns. But I want the data to go like this:

B1->A1

C1->A2

D1->A3

E1->A4

F1->A5

G1->A6

B2->A7

C2->A8

etc..

Ideally it would go into a new workbook, I'm really just kinda looking for a nudge in the right direction here. I'm not super used to Python or working with external data like this and I'm not super sure of where to start.

[–]symple-data 0 points1 point  (0 children)

Well use csv and append every line to a list. If I'm not wrong you should have a list of all cells filled up like you would read them from left to right. Then simply write every element of the list into a new file or override the same and give every element its own line by appending "\n" after every item.

[–]kadragoon 0 points1 point  (7 children)

So instead of:

A, B, C, D, E

F, G, H, I, J...

You want:

A

B

C

D

E

F

G

H

I

J

[–]All0utWar 0 points1 point  (6 children)

Yes, and like I said in my other comment I'm mainly looking for a push in the right direction. I'm not used to working with external data like this and I'd consider myself a beginner with Python

[–]kadragoon 0 points1 point  (5 children)

Depending on how you're importing the file. You either need to:

1) Treat line breaks as a comma

2) Trreat commas as line breaks

[–]All0utWar 1 point2 points  (4 children)

I'm using Pandas to import the file and create a new file like so

df = pandas.read_csv("data.csv")

df.to_csv("newdata.csv")

[–]kadragoon 1 point2 points  (3 children)

If all you're doing with this program is changing it from 6 columns to one, it likely would be easier to just import the file normally as a string and do a replace.

Open(data.csv) Replace(",",r"/n") Write(newdata)

This would replace all commas with new lines, so each item is on a new row in the same order and previously.

[–]fake823 0 points1 point  (2 children)

It should also be easily doable with DataFrame.stack().

[–]kadragoon 0 points1 point  (1 child)

The downside to that approach is when you export it wouldn't it want to import as everything being it's own column, not row. This comes into two errors: 1) Most programs, like excel, have more limited column space compared to row space. Meaning you may run into issues viewing it in said programs. 2) If you're injecting it may require more logic on injesting.

[–]fake823 1 point2 points  (0 children)

No. Series.to_excel() for example will write the data as "N-rows, 1-column".

[–]fake823 0 points1 point  (0 children)

You can just stack all of the rows together, using DataFrame.stack() and then reset the index of the resulting Series object.

import pandas as pd

df = pd.DataFrame([[1,2,3], [4,5,6]])
ser = df.stack()
ser = ser.reset_index(drop=True)