Home > Back-end >  How to stack columns/series vertically in pandas
How to stack columns/series vertically in pandas

Time:07-13

I currently have a dataframe that looks something like this

entry_1      entry_2
2022-01-21   2022-02-01
2022-03-23   NaT
2022-04-13   2022-06-06

however I need to vertically stack my two columns to get something like this

entry
2022-01-21
2022-03-23
2022-04-13
2022-02-01
NaT
2022-06-06

I've tried using df['entry'] = df['entry_1].append(df['entry_2']).reset_index(drop=True) to no success

CodePudding user response:

I recommend that you use

pd.DataFrame(df.values.ravel(), columns=['all_entries'])

This will allow you to return the flattened underlying data as an ndarray. By wrapping that in pd.Dataframe() you will convert it back to a dataframe with the column name "all_entries"

For more information please visit the pandas doc: https://pandas.pydata.org/docs/reference/api/pandas.Series.ravel.html

CodePudding user response:

You can convert to numpy.array and use numpy.ravel.

pd.DataFrame(df.values.ravel(), columns=['entry'])

    entry
0   2022-01-21
1   2022-02-01
2   2022-03-23
3   NaT
4   2022-04-13
5   2022-06-06

CodePudding user response:

You can use concat of series and get the result in a dataframe like:

df['entry_1'] = pd.to_datetime(df['entry_1'])
df['entry_2'] = pd.to_datetime(df['entry_2'])
df_result = pd.DataFrame({
               'entry':pd.concat([df['entry_1'], df['entry_2']], ignore_index=True)
            })

or

entry_cols = ['entry_1', 'entry_2']
df_result = pd.DataFrame({
       'entry':pd.concat([df[col] for col in entry_cols], ignore_index=True)
 })

print(df_result)

       entry
0 2022-02-21
1 2022-02-23
2 2022-04-13
3 2022-02-01
4        NaT
5 2022-06-06
  • Related