熊猫:根据系列值将列添加到DataFrame

时间:2020-03-25 08:27:47

标签: python pandas dataframe

我执行了groupby,这给了我一个pd.Series对象。像这样:

import pandas as pd
spanish = pd.Series(['uno', 'dos', 'tres'], ['one', 'two', 'three'])
>>> spanish
    one       uno
    two       dos
    three    tres
    dtype: object

我有原始的pd.DataFrame,其中一列的索引与pd.Series相匹配:

df = pd.DataFrame({'german': ['eins', 'zwei', 'drei'],
                   'english': ['one', 'two', 'three']},
                  index=[1, 2, 3])
>>> df
      german english
    1   eins     one
    2   zwei     two
    3   drei   three

从上面可以看出,english列与spanish系列中的索引匹配。我现在想将spanish的值添加到df中。换句话说,我想得到这个:

>>> df
      german english spanish
    1   eins     one     uno
    2   zwei     two     dos
    3   drei   three    tres

注意:我尝试在迭代时使用loc来分配spanish[df['english'][i]],但是此过程非常缓慢(我的真实DataFrame大约有600万个条目)。此外,我无法使用groupby.transform(),因为我正在对另一个DataFrame进行分组。最后,我不能使用索引(0、1、2),因为它们也不匹配。

谢谢。

0 个答案:

没有答案