Home > Back-end >  How to merge these 2 dataframes with pandas efficiently?
How to merge these 2 dataframes with pandas efficiently?

Time:08-19

I need to merge the label dataframe to the feature dataframe like this:

Label Dataframe:

Time (integer) Label
0 a
1 b
2 c
... ...

Feature Dataframe:

Time (float) Feature1 Feature2
0 ... ...
0.5 ... ...
1 ... ...
1.5 ... ...
2 ... ...
... ... ...

the result dataframe should be something like this:

Time (float) Feature1 Feature2 Label
0 ... ... a
0.5 ... ... a
1 ... .. b
1.5 ... ... b
2 ... ... c
... ... ... ...

which means, for example in 0~1s, the label is a; in 1~2s,the label is b; etc.

I have 100,000 rows in the actural dataframe. Is there any efficient way to do this?

CodePudding user response:

One option is

df2['Label'] = df2['Time'].astype(int).map(df1.set_index('Time').squeeze())

CodePudding user response:

You can try this: df1.merge(df2)

The official documentation

  • Related