1

I have the following data set:

1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28

I want to get the following result:

1
2
3
4
5
6
7
8
...
23
24
25
26
27
28

So I want to loop over all columns of my dataset and concat each column to the first one.

import pandas as pd

df = pd.read_csv("data.csv", delimiter=";", header=-1)

number_of_columns= len(df.columns)
print(number_of_columns)


for i in range (1,number_of_columns):
  df1 = df.iloc[:,i]
  df2 = pd.concat([df,df1], ignore_index=True)


print(df2)

With this only the last column is concatenated in the final dataframe. I get it that df2 is overwritten in each iteration of the for-loop.

So how can I "save" df2 after each for loop so that every column is concantenated?

Thanks a lot!

1
  • 1
    Use df.unstack() Commented Jul 28, 2018 at 13:03

4 Answers 4

3

For column-wise

stack+tolist

df.stack().tolist()

[1,
 8,
 15,
 22,
 2,
 9,
 16,
 23,
 3,
 10,
 17,
 24,
 4,
 11,
 18,
 25,
 5,
 12,
 19,
 26,
 6,
 13,
 20,
 27,
 7,
 14,
 21,
 28]

For row-wise

melt

df.melt().value.tolist()

[1,
 2,
 3,
 4,
 5,
 6,
 7,
 8,
 9,
 10,
 11,
 12,
 13,
 14,
 15,
 16,
 17,
 18,
 19,
 20,
 21,
 22,
 23,
 24,
 25,
 26,
 27,
 28]

unstack+tolist

df.unstack().tolist()

#outputs same as above
Sign up to request clarification or add additional context in comments.

Comments

2

simply np.flatten(),

 pd.Series(df.values.flatten())
 (or)
 pd.Series(df.unstack().values)

Comments

1

You can also do it:

txt = '''1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28'''
arr1 = np.fromstring(txt, dtype=int, sep=' ')
arr1.reshape(7,-1).flatten(order = 'F') # for column wise, 'C' can be used for row wise.

Comments

1

You may not require a 3rd party library. You can use csv and itertools modules from the standard library to return a list of numbers:

from io import StringIO
from itertools import chain
import csv

mystr = StringIO("""1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28""")

with mystr as fin:
    reader = csv.reader(mystr, skipinitialspace=True, delimiter=' ')
    res = list(map(int, chain.from_iterable(zip(*reader))))

print(res)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
 21, 22, 23, 24, 25, 26, 27, 28]

Comments

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.