1

I have a pretty simple Pandas question that deals with merging two series. I have two series in a dataframe together that are similar to this:

     Column1     Column2
0        Abc         NaN
1        NaN         Abc
2        Abc         NaN
3        NaN         Abc
4        NaN         Abc

The answer will probably end up being a really simple .merge() or .concat() command, but I'm trying to get a result like this:

     Column1
0        Abc
1        Abc
2        Abc
3        Abc
4        Abc

The idea is that for each row, there is a string of data in either Column1, Column2, but never both. I did about 10 minutes of looking for answers on StackOverflow as well as Google, but I couldn't find a similar question that cleanly applied to what I was looking to do.

I realize that a lot of this question just stems from my ignorance on the three functions that Pandas has to stick series and dataframes together. Any help is very much appreciated. Thank you!

1 Answers1

2

You can just use pd.Series.fillna:

df['Column1'] = df['Column1'].fillna(df['Column2'])

Merge or concat are not appropriate here; they are used primarily for combining dataframes or series based on labels.

Use groupby with first

df.groupby(df.columns.str[:-1],axis=1).first()
Out[294]: 
  Column
0    Abc
1    Abc
2    Abc
3    Abc
4    Abc

Or :

`ndf = pd.DataFrame({'Column1':df.fillna('').sum(1)})`
Bharath M Shetty
  • 30,075
  • 6
  • 57
  • 108
jpp
  • 159,742
  • 34
  • 281
  • 339
  • That worked perfectly! Thanks so much-- It says I have to wait 10 minutes to mark it as a correct answer. I'll do that shortly. – Matt Youngberg Jun 27 '18 at 15:12