gpt4 book ai didi

python - 如果逗号在 pandas 数据框列中开始一行,则删除逗号

转载 作者:行者123 更新时间:2023-11-30 21:54:00 24 4
gpt4 key购买 nike

假设这是 panda 数据框中的一列:

,12022, 20230, 41454
431202, 20230, 41454
,34343, 1332, 445453
...

有没有办法删除列中每行开头的逗号?

df_new[column_name1] = df_new[column_name].remove_comma_if_starts_string

获取:

12022, 20230, 41454
431202, 20230, 41454
34343, 1332, 445453
...

最佳答案

您可以使用 str.lstrip(',') 来实现此目的。它从左侧“剥离”给定的字符串/字符。与 str.rstrip() 相反,后者从右侧执行此操作。 str.strip() 在左侧和右侧都执行此操作。

df_new[column_name1] = df_new[column_name].str.lstrip(',')

关于python - 如果逗号在 pandas 数据框列中开始一行,则删除逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59438641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com