熊猫记录中的破折号后如何使小写字母变大?

青葱年少 python 198

原文标题How to make a small letter large after a dash in pandas records?

我在熊猫中有条目,我需要在破折号后将小写字母更改为大写字母。最好将这种变化记录在单独的列中。

示例数据框:

data        columns_1

2020-05-20  test-word

需要结果:

data        columns_1    columns_2

2020-05-20  test-word    Test-Word

我发现可以使用以下方法更改第一个字母:

df['columns_2'] = df['columns_1'].apply(lambda x: x.capitalize())

是否可以在此条目中添加一些内容,或者是否有其他方法可以做到这一点?

原文链接:https://stackoverflow.com//questions/71910210/how-to-make-a-small-letter-large-after-a-dash-in-pandas-records

回复

我来回复
  • mozway的头像
    mozway 评论

    你需要str.title

    df['columns_2'] = df['columns_1'].str.title()
    

    输出:

             data  columns_1   column-2
    0  2020-05-20  test-word  Test-Word
    
    2年前 0条评论
  • S Rawson的头像
    S Rawson 评论

    在已有内容的基础上,创建一个列表并加入列表中的单词将产生所需的结果:

    df['columns_2'] = df['columns_1'].apply(lambda x: "-".join([p.capitalize() for p in x.split("-")]))
    
    2年前 0条评论