user11980317
user11980317

Reputation:

How to append multiple columns values into a single column without append function?

I have a data which consist of 16310 columns x 6000 rows. I wanted to append all columns value into one columns. Let say

c1 c2
2  3
5  4
1  2

I wanted output like this

c1
2
5
1
3
4
2

I have done this using append function and it's working fine.

acc_y_c = acc_y[0]
for i in range(1, len(acc_y.columns)):
    acc_y_c = acc_y_c.append(acc_y[i])

But the issue is that it's taking to much time as I said above the data is consist of 16310 columns x 6000 rows. I wanted to know is there any method which take less time as compared to above method?

Upvotes: 0

Views: 295

Answers (1)

James
James

Reputation: 36598

Do you want .melt()?

df = pd.DataFrame({'c1': [2, 5, 1], 'c2': [3, 4, 2]})
df.melt(value_name='c1')
# returns:
  variable     c1
0       c1      2
1       c1      5
2       c1      1
3       c2      3
4       c2      4
5       c2      2

Timed Example:

data = [[np.random.randint(10) for _ in range(6000)] for _ in range(16310)]
df = pd.DataFrame(data)

Melt:

%%time
df_melt = df.melt()

>>> Wall time: 978 ms

Append:

%%time
acc_y_c = df[0]
for i in range(1, len(df.columns)):
    acc_y_c = acc_y_c.append(df[i])

>>> Wall time: 19min 15s

Upvotes: 1

Related Questions