dfafa
dfafa

Reputation: 45

Moving rows from one column to another along with respective values in pandas DataFrame

This is the dataframe I have with three rows and three columns.

a   d   aa
b   e   bb
c   f   cc


What I want is to remove the second column and adding those values to the rows in first column along with their respective values from third column.


This is the expected result:

a   aa
b   bb
c   cc
d   aa
e   bb
f   cc

Upvotes: 1

Views: 1234

Answers (3)

LevB
LevB

Reputation: 953

Here are 4 steps: split into 2 dataframes; make column names the same; append; reindex.

Import pandas as pd
df = pd.DataFrame({'col1':['a','b','c'],'col2':['c','d','e'],'col3':['aa','bb','cc']})

df2 = df[['col1','col3']] # split into 2 dataframes
df3 = df[['col2','col3']]
df3.columns = df2.columns # make column names the same
df_final = df2.append(df3) # append 
df_final.index = range(len(df_final.index)) # reindex
print(df_final)

Upvotes: 1

Amit Kharel
Amit Kharel

Reputation: 154

Firstly concat the columns:

df1 = pd.concat([df[df.columns[[0,2]]], df[df.columns[[1,2]]]])

Then what you obtain is:

    0   1   2
0   a   NaN aa
1   b   NaN bb
2   c   NaN cc
0   NaN d   aa
1   NaN e   bb
2   NaN f   cc

Now, just replace the NaN values in [0] with the corresponding values from [1].

df1[0] = df1[0].fillna(df1[1])

Output:

    0   1   2
0   a   NaN aa
1   b   NaN bb
2   c   NaN cc
0   d   d   aa
1   e   e   bb
2   f   f   cc

Here, you may only need [0] and [2] columns.

df1[[0,2]]

Final Output:

    0   2
0   a   aa
1   b   bb
2   c   cc
0   d   aa
1   e   bb
2   f   cc

Upvotes: 2

Igor Rivin
Igor Rivin

Reputation: 4864

pd.concat([df[df.columns[[0, 2]]], df[df.columns[[0, 1]]])

Upvotes: 0

Related Questions