1

I have this problem with Pandas as from a 'csv' file with multiple columns I would like to generate a new 'csv' file with a single column containing all the values as in the example below:

From:

column 1, column 2, column 3

1, 2, 3,

4, 5, 6

I would like to obtain:

column 1

1

2

3

4

5

6

Thank you all in advance

  • related : https://stackoverflow.com/questions/28654047/pandas-convert-some-columns-into-rows , `df.melt().loc[:,['value']]` – anky Mar 05 '21 at 17:47

2 Answers2

1

You can use ravel on DataFrame values:

pd.DataFrame({'column': df.values.ravel()})

Output:

   column
0       1
1       2
2       3
3       4
4       5
5       6
perl
  • 9,826
  • 1
  • 10
  • 22
0

you can try this

import pandas as pd

df = pd.DataFrame({"column1": [1, 4], "column2": [2, 5], "column3": [3, 6]})
print("df : \n", df)

reshaped_value = df.values.reshape(-1)
new_df = pd.DataFrame(columns=["column1"])

new_df["column1"] = reshaped_value
print("\nnew_df : \n", new_df)

output :

df : 
    column1  column2  column3
0        1        2        3
1        4        5        6

new_df : 
    column1
0        1
1        2
2        3
3        4
4        5
5        6
Vatsal Parsaniya
  • 899
  • 6
  • 15