简体   繁体   中英

How to merge these 2 dataframes with pandas efficiently?

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?

One option is

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

You can try this: df1.merge(df2)

The official documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM