【问题标题】:Pandas set a separator after the space熊猫在空格后设置分隔符
【发布时间】:2021-12-19 20:39:54
【问题描述】:

我有一个结构如下的文本文件:

  test1 test2                         data      world                        world          int             float          
  test3 test4 test5 test6             data      world                        world          int             float       
  test7 test8 test9                   data      world                        world                          float       
  test10 test11                       data      world         number         world          int             float  
  test12 test13                       data                    number         world          int             float 

我应该设置一个分隔符,前几个单词只用空格隔开,其他的都是唯一的,但不幸的是并不总是重复出现

我只是尝试在三个空格后替换逗号并逐渐缩放逗号,但在缺少中间单词的地方我少了一个逗号!

我尝试用 jupyter 和 pandas 像这样导入它:

df = pd.read_csv(r'C:test.csv', sep = '\n',header=None,encoding = "utf-8" )
ad
df = df[0].str.split('\t', expand=True)
or
df = pd.read_csv(r'C:test.csv', sep = '\t' )

但是没有什么不能识别选项卡和空格会弄乱整个数据框 我以为这需要一段时间,但我卡住了 有没有人有什么建议! 谢谢

【问题讨论】:

  • 字段之间的实际分隔符是什么?可以制表符分隔吗?
  • 还是固定宽度的字段?
  • 不,不幸的是它们既不是标签也没有固定宽度!
  • 那我觉得你完蛋了。它怎么知道最后一行的number是字段4而不是字段3?
  • 恐怕...而且我认为这很容易

标签: python regex pandas jupyter-notebook


【解决方案1】:

不是为了做广告,而是在经过 1 天的混乱之后,我通过 EmEditor 通过垂直编辑功能成功了 感谢您有 cmets 的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2018-06-21
    • 1970-01-01
    • 2020-04-09
    • 2021-02-06
    • 1970-01-01
    • 2012-08-14
    相关资源
    最近更新 更多