Home > Software engineering >  How to merge dataframe and series?
How to merge dataframe and series?

Time:01-17

I have a dataframe and a series which are as following.

_df = pd.DataFrame({'a': [1, '', '', 4], 'b': ['apple', 'banana', 'orange', 'pear']}, columns=['a', 'b'])
_series = pd.Series(['', 2, 3, ''], name="a")

Here I would like to merge the dataframe and series along column a to get rid of all the blanks. This is the result I want.

   a    b
0  1  apple
1  2  banana
2  3  orange
3  4  pear

Here is how I do it.

for i in range(len(_df.iloc[:, 0].to_list())):
    if _df.iloc[i, 0] == '':
       _df.iloc[i, 0] = df_series[i]

Problem is it can be very slow if the dataframe is big. Anyone knows if I can do this in a more efficient way?

CodePudding user response:

Use result

  • Related