【问题标题】:remove first comma of one column with Pandas用 Pandas 删除一列的第一个逗号
【发布时间】:2018-11-23 22:12:33
【问题描述】:

如果我有如下数据框:

id         fruits
01     ,Apple, Apricot
02     ,Apple, Banana, Clementine, Pear
03     ,Orange, Pineapple, Pear

如何删除第一个逗号并获得这样的新数据框:

id      fruits
01     Apple, Apricot
02     Apple, Banana, Clementine, Pear
03     Orange, Pineapple, Pear

【问题讨论】:

  • 总是第一个值 , ?如果是,那就是骗人的。
  • 是的..对不起,我没有找到另一个链接。

标签: python pandas


【解决方案1】:

使用str.lstrip:

df['fruits'] = df['fruits'].str.lstrip(',')
print (df)
   id                           fruits
0   1                   Apple, Apricot
1   2  Apple, Banana, Clementine, Pear
2   3          Orange, Pineapple, Pear

【讨论】:

    【解决方案2】:
    df['fruits'] = df['fruits'].str[1:]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 2013-12-03
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多