Merging a column from one dataframe to another in which only one index column has to be used in pandas

Question:

I have two data frames that look like this, data_df is

                             Amplitude (V)  Noise (V)  Rise time (s)  ...
n_trigger signal_name                                                 ...
0         reference_trigger       0.123044   0.001194   3.394432e-10  ...
          DUT                     0.260159   0.003952   6.088619e-10  ...
1         reference_trigger       0.245865   0.001394   2.640444e-10  ...
          DUT                     0.230720   0.002880   5.774005e-10  ...
2         reference_trigger       0.089424   0.001273   2.786659e-10  ...
...                                    ...        ...            ...  ...
6663      DUT                     0.332948   0.004620   6.000350e-10  ...
6664      reference_trigger       0.240404   0.001109   2.693422e-10  ...
          DUT                     0.229863   0.002180   6.013701e-10  ...
6665      reference_trigger       0.086362   0.001335   3.179252e-10  ...
          DUT                     0.195570   0.003701   6.279319e-10  ...

and filter_df is

           accepted
n_trigger          
0              True
1              True
2              True
3              True
4              True
...             ...
6661           True
6662           True
6663           True
6664           True
6665           True

I want to append a new column accepted to data_df given by filter_df in the obvious way, i.e. using the index n_trigger accordingly. I tried

data_df['accepted'] = filter_df['accepted']

but does not work. One way of achieving what I want to do is like this:

data_df.reset_index(inplace=True, drop=False)
data_df.set_index('n_trigger', inplace=True)
data_df['accepted'] = filter_df['accepted']
data_df.reset_index(inplace=True, drop=False)
data_df.set_index(['n_trigger','signal_name'], inplace=True)

Though this works, it does not feel like the way to go since I am doing a mess with the index. How can I do this operation without dropping the index and creating it again afterwards?

I guess there might be something like data_df = pandas.concat([data_df,filter_df], axis=1, using_index_named='n_trigger') but cannot find it.

Asked By: user171780

||

Answers:

Try this:

data_df = data_df.assign(accepted=filter_df['accepted'])
Answered By: Mujtaba Mateen

You need to merge:

data_df = data_df.merge(right=filter_df, left_index=True, right_index=True)
Answered By: gtomer