Home > Software design >  How to melt multiple columns into one without the column names
How to melt multiple columns into one without the column names

Time:12-13

I have a pandas dataframe with multiple columns and I would like to create a new dataframe by flattening all columns into one using the melt function. But I do not want the column names from the original dataframe to be a part of the new dataframe.

Below is the sample dataframe and code. Is there a way to make it more concise?

date                   Col1      Col2       Col3      Col4           
1990-01-02 12:00:00     24        24        24.8      24.8           
1990-01-02 01:00:00     59        58        60        60.3   
1990-01-02 02:00:00     43.7      43.9      48        49

The output desired:

    Rates
0   24
1   59
2   43.7
3   24
4   58
5   43.9
6   24.8
7   60
8   48
9   24.8
10  60.3
11  49

Code :

df = df.melt(var_name='ColumnNames', value_name='Rates')  #using melt function to flatten columns
df_main.drop(['ColumnNames'], axis = 1, inplace = True)   # dropping 'ColumnNames'

CodePudding user response:

Set value_name and value_vars params for your purpose:

In [137]: pd.melt(df, value_name='Price', value_vars=df.columns[1:]).drop('variable', axis=1)
Out[137]: 
    Price
0    24.0
1    59.0
2    43.7
3    24.0
4    58.0
5    43.9
6    24.8
7    60.0
8    48.0
9    24.8
10   60.3
11   49.0

CodePudding user response:

As an alternative you can use stack() and transpose():

dfx = df.T.stack().reset_index(drop=True) #date must be index.

Output:

    0
0   24.0
1   59.0
2   43.7
3   24.0
4   58.0
5   43.9
6   24.8
7   60.0
8   48.0
9   24.8
10  60.3
11  49.0

  • Related