Platalea Minor
Platalea Minor

Reputation: 877

Combine multi columns to one column Pandas

Hi I have the following dataframe

   z  a   b   c 
   a  1   NaN NaN
   ss NaN 2   NaN
   cc 3   NaN NaN
   aa NaN 4   NaN
   ww NaN 5   NaN
   ss NaN NaN 6
   aa NaN NaN 7
   g  NaN NaN 8
   j  9   NaN NaN

I would like to create a new column d to do something like this

z  a   b   c    d
a  1   NaN NaN  1
ss NaN 2   NaN  2
cc 3  NaN NaN  3
aa NaN 4   NaN  4
ww NaN 5   NaN  5
ss NaN NaN 6    6
aa NaN NaN 7    7
g  NaN NaN 8    8
j  9   NaN NaN  9

For the numbers, it is not in integer. It is in np.float64. The integers are for clear example. you may assume the numbers are like 32065431243556.62, 763835218962767.8 Thank you for your help

Upvotes: 0

Views: 271

Answers (4)

A. AlBoori
A. AlBoori

Reputation: 11

if there is only one value per row as given example, you can use the code below to dropna for each row and assign the remaining value to column d

df['d']=df.apply(lambda row: row.dropna(), axis=1)

Upvotes: 0

jpp
jpp

Reputation: 164623

You can use pd.DataFrame.ffill over axis=1:

df['D'] = df.ffill(1).iloc[:, -1].astype(int)

print(df)

     a    b    c  D
0  1.0  NaN  NaN  1
1  NaN  2.0  NaN  2
2  3.0  NaN  NaN  3
3  NaN  4.0  NaN  4
4  NaN  5.0  NaN  5
5  NaN  NaN  6.0  6
6  NaN  NaN  7.0  7
7  NaN  NaN  8.0  8
8  9.0  NaN  NaN  9

Of course, if you have float values, int conversion is not required.

Upvotes: 0

smartass
smartass

Reputation: 26

In fact, it's not nessary to use fillna, sum can transform the NAN elements to zeros automatically.

I'm a python newcomer as well,and I suggest maybe you should read the pandas cookbook first.

The code is:

df['Total']=df[['a','b','c']].sum(axis=1).astype(int)

Upvotes: 1

phi
phi

Reputation: 11704

We can replace the NA by 0 and sum up the rows.

df['d'] = df[['a', 'b', 'c']].fillna(0).sum(axis=1)

Upvotes: 1

Related Questions