【问题标题】:Create column from a substring of another column从另一列的子字符串创建列
【发布时间】:2018-09-23 06:44:45
【问题描述】:

我有一个 Pandas 数据框对象。我想从现有列的子字符串创建新列。我的数据如下所示:

    Date        variable                 want1          want2   want3
0   02-01-08    Australia - Sydney - A   Australia      Sydney  A
1   03-01-08    Australia - Sydney - A   Australia      Sydney  A
2   04-01-08    Australia - Sydney - A   Australia      Sydney  A
3   05-01-08    Canada - Toronto - B     Canada         Toronto B
4   06-01-08    Canada - Toronto - B     Canada         Toronto B

want1want3 是我需要的。

【问题讨论】:

    标签: python python-3.x pandas string dataframe


    【解决方案1】:

    您可以为此使用pd.Series.str.split

    df[['want1', 'want2', 'want3']] = df['variable'].str.split(' - ', expand=True)
    

    【讨论】:

    • 谢谢。如果我只想要前 2 列怎么办?
    【解决方案2】:

    pd.Series.extract

    pat = '(?P<want1>.*) - (?P<want2>.*) - (?P<want3>.*)'
    df.join(df.variable.str.extract(pat, expand=True))
    
           Date                variable      want1    want2 want3
    0  02-01-08  Australia - Sydney - A  Australia   Sydney     A
    1  03-01-08  Australia - Sydney - A  Australia   Sydney     A
    2  04-01-08  Australia - Sydney - A  Australia   Sydney     A
    3  05-01-08    Canada - Toronto - B     Canada  Toronto     B
    4  06-01-08    Canada - Toronto - B     Canada  Toronto     B
    

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 2016-10-02
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      相关资源
      最近更新 更多