Home > Software design >  add one column to the other columns of the same dataframe in sequence
add one column to the other columns of the same dataframe in sequence

Time:11-09

I have a dataframe like this

column1 column2  column3 column4...columnn
1        xx       cc      Nan       aa
2        xx       cc      gg        aa
3        xx       Nan     gg        aa

What I want to do is to add column1 to the other column in parallel and delete column1. I do not want to add column1 where values are Nan

So I want as output the following

 column2  column3 column4...columnn
   1xx       1cc      Nan... 1aa
   2xx       2cc      2gg... 2aa
   3xx       Nan      3gg... 3aa

Any ideas?

CodePudding user response:

df['column2'] =   df['column1']   df['column2']
df['column3'] =   df['column1']   df['column3']
df['column4'] =   df['column1']   df['column4']
df.drop(columns=['column1'])

CodePudding user response:

Tauqeer Akhtar has the right idea but you need to typecast it to string before concatenation.

column1 = df['column1'].astype(str)
df.loc[df['column2'].notna(), 'column2'] = column1   df['column2']
df.loc[df['column3'].notna(), 'column3'] = column1   df['column3']
df.loc[df['column4'].notna(), 'column4'] = column1   df['column4']
del column1
df.drop(['column1'], axis=1)

or borrowing mcsoinis solution

(df.loc[:, df.columns != "column1"]
   .apply(lambda x: np.where(x.isna(), np.nan, df["column1"].astype(str)   x), axis=0))

CodePudding user response:

Use apply with axis 0 to perform the same operation on each column or a selection of columns:

(df.loc[:, df.columns != "column1"]
   .apply(lambda x: df["column1"].astype(str)   x, axis=0))

# Out:
  column2 column3 column4
0     1xx     1cc     1gg
1     2xx     2cc     2gg
2     3xx     3cc     3gg
  • Related