pandas 如何按照另一个 Dataframe 的顺序对一个 Dataframe 进行排序?

lo8azlld  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(143)

我有两个 Dataframe 。eval_dfuneval_df
eval_df看起来像这样:

sentiment            id                       date                                               text
0             0  5.400000e+17  2014-12-03 07:01:05+00:00  conniempp wwi began in sarajevo wwiii to begin...
1             0  1.320000e+18  2020-10-24 17:05:38+00:00  nsheth4 janet63388900 alivelshi pretty sure yo...
2             0  6.420000e+17  2015-09-11 16:33:44+00:00  immigration is stressful for all immigrants go...
3             0  1.080000e+18  2019-01-07 15:39:48+00:00  motorcitihawk relbtx ingrahamangle wolfblitzer...
4             1  4.360000e+17  2014-02-20 02:41:36+00:00  christiisilvaxx you can get somethin better wi...
...         ...           ...                        ...                                                ...
62744         0  1.310000e+18  2020-09-14 15:58:18+00:00  seminoles78 grandpasnarky they buy in to the i...
62745         0  1.470000e+18  2021-12-17 02:27:04+00:00  michelecomstoc1 farmerrick potus 2this resulte...
62746         0  1.280000e+18  2020-06-22 16:09:31+00:00  i have this lady on the phone and her english ...
62747         0  1.140000e+18  2019-06-13 03:21:33+00:00  food just has not been the same since i left v...
62748         0  1.280000e+18  2020-07-18 00:02:24+00:00  half of this country was mexico and was built ...

uneval_df看起来像这样:

sentiment            id                       date                                               text
0             0  7.471049e+17  2016-06-26 16:31:07+00:00  if 12 million immigrants from mexico will help...
1             0  8.338858e+17  2017-02-21 03:47:19+00:00  russian immigrants in minnesota are perplexed ...
2             0  1.227999e+18  2020-02-13 16:52:33+00:00  washingtonpost gather prison all near border b...
3             0  1.346244e+18  2021-01-04 23:57:07+00:00  serenitydogg sethabramson i hope he runs to ru...
4             0  6.356885e+17  2015-08-24 05:42:03+00:00  realdonaldtrump doesnt matter if our parents m...
...         ...           ...                        ...                                                ...
62744         0  1.572305e+18  2022-09-20 19:21:44+00:00  real original americans the native american in...
62745         0  1.374097e+18  2021-03-22 20:34:53+00:00  fairness bill is good for america silence is n...
62746         0  6.664505e+17  2015-11-17 02:59:04+00:00  if christians are still in syria why are they ...
62747         0  1.373095e+18  2021-03-20 02:11:41+00:00  mbachelet no to ethiopia human right commissio...
62748         0  9.221188e+17  2017-10-22 15:13:54+00:00  scottyhasty jbouie hahah and i bet you think i...

eval_df的顺序是特定的、随机的,不应更改。如何按照与eval_df相同的顺序对uneval_df进行排序,以使各行的顺序相同?
预期结果:我希望uneval_df看起来像这样:

sentiment            id                       date                                               text
0             0  5.400000e+17  2014-12-03 07:01:05+00:00  began in sarajevo to begin...
1             0  1.320000e+18  2020-10-24 17:05:38+00:00  alivelshi pretty sure yo...
2             0  6.420000e+17  2015-09-11 16:33:44+00:00  immigration is stressful for all immigrants go...
3             0  1.080000e+18  2019-01-07 15:39:48+00:00  wow that was cool
4             0  4.360000e+17  2014-02-20 02:41:36+00:00  you can get somethin better wi...
...         ...           ...                        ...                                                ...
62744         0  1.310000e+18  2020-09-14 15:58:18+00:00  they buy in to the i...
62745         0  1.470000e+18  2021-12-17 02:27:04+00:00  2this resulted in something...
62746         0  1.280000e+18  2020-06-22 16:09:31+00:00  i have this lady on the phone and her english ...
62747         0  1.140000e+18  2019-06-13 03:21:33+00:00  food just has not been the same since i left v...
62748         0  1.280000e+18  2020-07-18 00:02:24+00:00  half of this country was mexico and was built ...

我试着按以下语句排序:

uneval_df = uneval_df.sort_values(by=eval_df['id'])

但这返回了以下错误:

raise KeyError(key)
KeyError: 0        5.400000e+17
1        1.320000e+18
2        6.420000e+17
3        1.080000e+18
4        4.360000e+17
             ...     
62744    1.310000e+18
62745    1.470000e+18
62746    1.280000e+18
62747    1.140000e+18
62748    1.280000e+18
Name: id, Length: 62749, dtype: float64
o7jaxewo

o7jaxewo1#

你能试试这个吗:

eval_df =eval_df.set_index('id')
eval_df = eval_df.reindex(index=uneval_df['id'])

相关问题