【问题标题】:Python/Pandas - Breaking string based on hyhen to two columns [duplicate]Python / Pandas - 基于连字符将字符串分解为两列[重复]
【发布时间】:2020-10-09 08:45:02
【问题描述】:

我在 Pandas 数据框中有一个以下列,其中包含邮政编码列 -

postal code    city     country
56789-2345     Dallas    USA
45675          Austin    USA
null           Houston   USA
23445-445      Dallas    USA
1234-45        Austin    USA
34567          Houston   USA

我需要将邮政编码分解为邮政编码和分机,如下所示。就像将字符串与连字符(-)分开 -

postal_code     postal_ext    city      country
56789           2345          Dallas     USA
45675                         Austin     USA
null                          Houston    USA
23445           445           Dallas     USA
1234            45            Austin     USA
34567                         Houston    USA

我该怎么做

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这是join

    out = df.pop('postal code').str.split('-',expand=True)
    out.columns = ['Postal code','postal_ext']
    out = out.join(df)
    

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 2017-09-17
      • 2018-09-22
      相关资源
      最近更新 更多