Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert subset of columns to rows by combining columns

Tags:

python

pandas

Pandas 1.1.4

MRE:

df = pd.DataFrame({"Code":[1,2], "view_A":[3000, 2300], "click_A":[3, 23], 
                   "view_B":[1200, 300], "click_B":[5, 3]})
df.set_index("Code", inplace=True)

>>>
      view_A    click_A     view_B  click_B
Code            
1     3000        3          1200      5
2     2300       23          300       3

Want to make it into

              view      click
Code  type 
 1     A      3000        3
 2     A      2300       23
 1     B      1200        5
 2     B      300         3

I can do it, but want to explore more (clean) options.

My sol'tn

a_df = df[["view_A", "click_A"]].rename(columns={"view_A":"view", "click_A":"click"})
a_df["type"] = "A"

b_df = df[["view_B", "click_B"]].rename(columns={"view_B":"view", "click_B":"click"})
b_df["type"] = "B"
final_df = pd.concat([a_df, b_df])

But code is dirty.

like image 683
haneulkim Avatar asked Oct 26 '21 02:10

haneulkim


People also ask

How do I convert columns to rows in pandas?

Method #2: Using pivot() method. In order to convert a column to row name/index in dataframe, Pandas has a built-in function Pivot. Now, let's say we want Result to be the rows/index, and columns be name in our dataframe, to achieve this pandas has provided a method called Pivot.

How do I combine multiple columns into one list in Python?

You can use DataFrame. apply() for concatenate multiple column values into a single column, with slightly less typing and more scalable when you want to join multiple columns .


2 Answers

This is essentially a reshape operation using stack

df.columns = df.columns.str.split('_', expand=True)
df.stack().rename_axis(['code', 'type'])

           click  view
code type             
1    A         3  3000
     B         5  1200
2    A        23  2300
     B         3   300
like image 78
Shubham Sharma Avatar answered Oct 23 '22 09:10

Shubham Sharma


try with pd.wide_to_long

out = pd.wide_to_long(df.reset_index(),
                      ['view','click'],
                      i='Code',
                      j='type',
                      sep='_',
                      suffix='\\w+')
           view  click
Code type             
1    A     3000      3
2    A     2300     23
1    B     1200      5
2    B      300      3
like image 6
BENY Avatar answered Oct 23 '22 09:10

BENY