How to add a pandas Series to a DataFrame ignoring indices?

Viewed 9495

I have a DataFrame with random, unsorted row indices, which is a result of removing some 'noise' from the original DataFrame.

row_index    col1 col2
        2    1    2
       19    3    4
      432    4    1

I would like to add some pd.Series to this Dataframe. The Series has its indices sorted from 0 to n=number of rows. The number of rows equals the number of rows in the DataFrame

Having tried multiple ways of adding the Series to my DataFrame I realized that the data from the Series gets mixed up, because (I believe) Python is matching records by their indices.

Is there a way I can add the Series to the Dataframe, ignoring the indices, so that my data doesn't get mixed up?

2 Answers

convert the series into a data frame.

code

df=pd.DataFrame(df)
result=pd.concat([df1,df],axis=1,ignore_index=True)

df1 is the data frame you want to add .

df is the data frame i.e series you converted to data frame

df['new_col'] = other_df['column'].values
Related