【问题标题】:Pandas - Data Cleaning - Add New Column with if else statement for text valuesPandas - 数据清理 - 使用 if else 语句为文本值添加新列
【发布时间】:2017-03-14 13:30:05
【问题描述】:

我有一个如下所示的发布者列表:

+--------------+
|  Site Name   |
+--------------+
| Radium One   |
| Euronews     |
| EUROSPORT    |
| WIRED        |
| RadiumOne    |
| Eurosport FR |
| Wired US     |
| Eurosport    |
| EuroNews     |
| Wired        |
+--------------+

我想创建以下结果:

+--------------+----------------+
|  Site Name   | Publisher Name |
+--------------+----------------+
| Radium One   | RadiumOne      |
| Euronews     | Euronews       |
| EUROSPORT    | Eurosport      |
| WIRED        | Wired          |
| RadiumOne    | RadiumOne      |
| Eurosport FR | Eurosport      |
| Wired US     | Wired          |
| Eurosport    | Eurosport      |
| EuroNews     | Euronews       |
| Wired        | Wired          |
+--------------+----------------+

我想了解如何复制我在 Power Query 中使用的代码:

搜索前 4 个字符

如果 Text.Start([Site Name],4) = "WIRE" then "Wired" else

搜索最后 3 个字符

if Text.End([Site Name],3) = "One" then "RadiumOne" else

如果没有找到匹配,则添加“Rest”

不必区分大小写。

【问题讨论】:

    标签: python pandas if-statement conditional rename


    【解决方案1】:

    我认为您可以使用双 numpy.whereindexing with str 创建的条件:

    s = df['Site Name'].str.lower()
    df['new'] = np.where(s.str[:4] == 'wire', 'Wired', 
                np.where(s.str[-3:] == 'one', 'RadiumOne', 'Rest'))
    

    但如果需要你的输出,还需要splittitle

    df['new1'] = np.where(s.str[:4] == 'wire', 'Wired', 
                 np.where(s.str[-3:] == 'one', 'RadiumOne', s.str.split().str[0].str.title()))
    
    print (df)
          Site Name        new       new1
    0    Radium One  RadiumOne  RadiumOne
    1      Euronews       Rest   Euronews
    2     EUROSPORT       Rest  Eurosport
    3         WIRED      Wired      Wired
    4     RadiumOne  RadiumOne  RadiumOne
    5  Eurosport FR       Rest  Eurosport
    6      Wired US      Wired      Wired
    7     Eurosport       Rest  Eurosport
    8      EuroNews       Rest   Euronews
    9         Wired      Wired      Wired
    

    【讨论】:

    • 这太棒了。因此,如果我理解正确,如果结果是 Rest,则拆分和标题将使用原始站点名称?最后一件事,有没有办法合并新列,例如'新',包含站点名称的现有数据框,但同时删除旧站点名称列?然后这些步骤将是一个 vlookup,在匹配后,它会自动删除/替换旧列
    • 不,有不同。我对两个单词名称使用拆分,如Eurosport FRsplit 由空格 - 得到eurosportfr,然后通过str[0] 选择第一个(eurosport)。 title 用于将第一个字母添加到大写 - 因此 eurosport 更改为 Eurosport
    • 如果Rest 到列new 并且如果使用拆分与标题输出在new1 中,我也会得到输出。但如果需要用新值覆盖旧列 Site Name,请使用 assign - df['Site Name'] = np.where(s.str[:4] == 'wire', 'Wired', np.where(s.str[-3:] == 'one', 'RadiumOne', s.str.split().str[0].str.title()))
    • 我认为最好的方法是对所有专栏都尝试一下——这样你就可以使用你的样本了——df = pd.DataFrame({'Site Name': {0: 'Radium One', 1: 'Euronews', 2: 'EUROSPORT', 3: 'WIRED', 4: 'RadiumOne', 5: 'Eurosport FR', 6: 'Wired US', 7: 'Eurosport', 8: 'EuroNews', 9: 'Wired'}}) print (df)
    • 然后测试我所有的代码每个部分 print (df['Site Name'].str.lower()) 转换为小写,print (df['Site Name'].str.lower().str.split()) 用空格分隔(默认分隔符),print (df['Site Name'].str.lower().str.split().str[0]) - 选择列表的第一个值,print (df['Site Name'].str.lower().str.split().str[0].str.title()) - 转换第一个字母大写
    【解决方案2】:

    您可以使用apply 方法和函数,例如:

    def handle_text(txt):
        if txt.lower()[:4] == 'wire':
            return 'Wired'
        elif txt.lower()[-3:] == 'one':
            return 'RadiumOne'
        return 'Rest'
    
    df['Publisher Name'] = df['Site Name'].apply(handle_text)
    

    【讨论】:

    • 嗨,Dmitry,感谢您的快速回复,当我使用您的度量进行合并时,有没有办法确保 elif txt 为空白,然后显示 Rest
    • handle_text 是常用的 Python 函数,它接受单个字符串,执行某些操作并返回字符串。当然你可以在里面做任何事情。
    猜你喜欢
    • 2021-12-31
    • 2020-02-21
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多