【问题标题】:string split with expand=True. Can anyone explain what is the meaning?使用 expand=True 分割字符串。谁能解释一下是什么意思?
【发布时间】:2020-12-26 23:33:39
【问题描述】:
all_data['Title']= all_data['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]

谁能解释一下这行代码是什么意思?尤其是 expand=True[1] [0]

【问题讨论】:

  • 这不是通常的字符串拆分方法,它是all_data['Name'].str返回的任何对象的特定方法。我相信您在这里使用的是 Pandas 数据框,您需要查看 Pandas 文档以了解其对象定义的方法以及它们采用的参数。
  • 添加的答案能否解决问题?考虑投票/接受。

标签: python pandas string split expand


【解决方案1】:

看这里:pandas.Series.str.split

将拆分的字符串展开成单独的列。

如果为 True,则返回 DataFrame/MultiIndex 扩展维度。

如果为 False,则返回包含字符串列表的系列/索引。

s = pd.Series(
    [
        "this is a regular sentence",
    ]
)    
s.str.split(expand=True)

0 1 2 3 4
这是一个普通的句子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    相关资源
    最近更新 更多