【问题标题】:How to replace multiple comma separated textual words in columns with categorical code [closed]如何用分类代码替换列中的多个逗号分隔的文本词[关闭]
【发布时间】:2019-10-21 07:45:20
【问题描述】:

您好,有这样的数据集:

user     read
Den    Insurance
Den    Utility
Mark   Power;Bonds;Corporates
Mark   Government
Celia  Retail
Celia  Technology;Paper
Celia  Food

我有另一个这样的数据集:

Name            Code
Insurance        1
Utility          2
Power            3
Bond             4
Corporates       5
Government       6
Retail           7
Technology       8
Paper            9
Food             10

我想将这些用于数据框并将第一个数据转换为:

user     read                  Code
Den    Insurance                1
Den    Utility                  2
Mark   Power;Bonds;Corporates  3,4,5
Mark   Government               6
Celia  Retail                   7
Celia  Technology;Paper        8,9
Celia  Food                     10

如何在 Python Dataframe 中做到这一点?

【问题讨论】:

    标签: python-3.x pandas numpy replace categories


    【解决方案1】:

    IIUC

    d = df1.set_index('Name').Code.astype(str)
    df0.assign(Code=[', '.join(map(d.get, s.split(';'))) for s in df0.read])
    
        user                    read     Code
    0    Den               Insurance        1
    1    Den                 Utility        2
    2   Mark  Power;Bonds;Corporates  3, 4, 5
    3   Mark              Government        6
    4  Celia                  Retail        7
    5  Celia        Technology;Paper     8, 9
    6  Celia                    Food       10
    

    【讨论】:

    • 收到此错误:TypeError: sequence item 0: expected str instance, Series found
    • 我不能说。我不确切知道您的数据是如何创建的。
    • 我会努力的。我认为你的逻辑会起作用。谢谢你的回答:)
    • 嘿,这可以修改为那些没有类别代码的吗?例如,如果出现“汽车”但它不在代码列表中,我们可以解决它吗?
    • 是的,只需在 map 中使用不同的可调用对象。改用 df0.assign(Code=[', '.join(map(lambda x: d.get(x, '-1'), s.split(';'))) for s in df0.read]) 这将在代码数据框中不存在代码时使用 '-1'
    【解决方案2】:

    我在这里使用unnesting作为你的第一个数据框,然后我们只需要相应地创建代码列,groupbyagg

    df.read=df.read.str.split(';')
    df=unnesting(df,['read'])
    df['Code']=df.read.map(df1.set_index('Name').Code)
    yourdf=df.astype(str).groupby(level=0).agg({'user':'first','read':';'.join,'Code':','.join})
    yourdf
    Out[255]: 
        user                    read   Code
    0    Den               Insurance      1
    1    Den                 Utility      2
    2   Mark  Power;Bonds;Corporates  3,4,5
    3   Mark              Government      6
    4  Celia                  Retail      7
    5  Celia        Technology;Paper    8,9
    6  Celia                    Food     10
    

    def unnesting(df, explode):
        idx = df.index.repeat(df[explode[0]].str.len())
        df1 = pd.concat([
            pd.DataFrame({x: np.concatenate(df[x].values)}) for x in explode], axis=1)
        df1.index = idx
    
        return df1.join(df.drop(explode, 1), how='left')
    

    【讨论】:

    • 嘿,这可以修改为那些没有类别代码的吗?例如,如果出现“汽车”但它不在代码列表中,我们可以解决它吗?
    猜你喜欢
    • 2014-07-29
    • 2017-10-01
    • 2015-08-18
    • 2012-04-09
    • 2020-01-21
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多