Copying text from one cell to another without deleting original content python

Viewed 50

I have 7 columns, with million rows of data for each column.

I need to copy the data from columns 2,3,4,5,6,7 and place it at the end of the content in column 1. This would result in column 1 having its original content (1 million of rows) plus the additional content of the rest of the columns (6 million of rows).

I would like to transform this:

A  B  C
1  4  7
2  5  8
3  6  9

Into this:

A
1
2
3
4
5
6
7
8
9

I tried using pandas, but it did not give me desired output. Instead of chronologically ordering it from 1 to 9, I got it row-ordered (1,4,7,2,5,8,3,6,9) using my code below. Any ideas how to do it properly?

import pandas as pd

df = pd.read_excel('filename.xlsx', sheet_name='Sheet1')
df = df.stack().reset_index(drop=True)


df.to_excel("output.xlsx")
1 Answers

Try pd.melt

df_new = pd.melt(df).drop("variable", axis=1)

print(df_new)

   value
0      1
1      2
2      3
3      4
4      5
5      6
6      7
7      8
8      9
Related