ur mom
ur mom

Reputation: 17

How to concatenate rows in python pd?

How can I concatenate two columns of a df into only one?

I've tried lots of possible combinations (with append, with np, with concat ) ... and there's always an error or the table outputs this way

`

   A    B

0  75   Nan

1  71   NaN

2  NaN    83

3  NaN    64

`

instead of in only 1 column

what do I have to do?

Upvotes: 0

Views: 60

Answers (1)

cmauck10
cmauck10

Reputation: 163

It depends on what data your columns contain and how you want to combine it.

The easiest way is to do:

df['new_column'] = df['column1'] + df['column2']

To solve the NaN issue you'll need to remove/replace them first. For example, if you are adding columns together, you can replace the NaN's with 0 so that they don't affect the sum.

df.fillna(0)

Upvotes: 2

Related Questions