Home > Enterprise >  Add two columns one below other in pandas
Add two columns one below other in pandas

Time:07-26

I have a big DataFrame and I`m trying to transform 2 columns(Series) into 1.

Example:

print(df['col1'])
0 1
1 2
2 3
print(df['col2'])
0 7
1 8
2 9

Output:

print(df['singleCol'])
0 1
1 2
2 3
3 7
4 8
5 9

Is this possible using Pandas?

CodePudding user response:

You can use:

df.stack().drop_duplicates().reset_index(drop=True)

example input:

df = pd.DataFrame({'col1': [1, 1, 2, 3], 'col2': [7, 8, 9, 9]})

output:

0    1
1    7
2    8
3    2
4    9
5    3
dtype: int64
alternative for a subset of columns:
df.melt('col3', value_name='singleCol').drop_duplicates('singleCol').drop(columns='variable')

example input:

df = pd.DataFrame({'col1': [1, 1, 2, 3], 'col2': [7, 8, 9, 9], 'col3': list('ABCD')})

output:

  col3  singleCol
0    A          1
2    C          2
3    D          3
4    A          7
5    B          8
6    C          9

CodePudding user response:

series1 = pd.Series(df['col1'])
series2 = pd.Series(df['col2'])
df = pd.concat([series1, series2], axis = 0)
  • Related