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.
CodePudding user response:
You need to merge:
data_df = data_df.merge(right=filter_df, left_index=True, right_index=True)
CodePudding user response:
Try this:
data_df = data_df.assign(accepted=filter_df['accepted'])
CodePudding user response:
If you use pd.concat()
you will also see those rows without matching, if you need it. For example
print(pd.concat([data_df, filter_df], axis=1))
I have removed row index 4, 5, 6661, 6662 from data_df
so you will get NaN
in those rows
Amplitude(V) Noise(V) Risetime(s) signal_name accepted
n_trigger
0 0.123044 0.001194 3.394432e-10 DUT True0
1 0.245865 0.001394 2.640444e-10 DUT True1
2 0.089424 0.001273 2.786659e-10 DUT True2
3 0.240404 0.001109 2.693422e-10 DUT True3
6663 0.332948 0.004620 6.000350e-10 DUT True8
6664 0.229863 0.002180 6.013701e-10 DUT True9
6665 0.195570 0.003701 6.279319e-10 DUT True10
4 NaN NaN NaN NaN True4
5 NaN NaN NaN NaN True5
6661 NaN NaN NaN NaN True6
6662 NaN NaN NaN NaN True7