import text to pandas with multiple delimiters

前端 未结 1 1630
忘了有多久
忘了有多久 2020-11-27 18:18

I have some data that looks like this:

c stuff
c more header
c begin data         
 1 1:.5
 1 2:6.5
 1 3:5.3

I want to import it into a 3 c

相关标签:
1条回答
  • 2020-11-27 18:57

    One way might be to use the regex separators permitted by the python engine. For example:

    >>> !cat castle.dat
    c stuff
    c more header
    c begin data         
     1 1:.5
     1 2:6.5
     1 3:5.3
    >>> df = pd.read_csv('castle.dat', skiprows=3, names=['a', 'b', 'c'], 
                         sep=' |:', engine='python')
    >>> df
       a  b    c
    0  1  1  0.5
    1  1  2  6.5
    2  1  3  5.3
    
    0 讨论(0)
提交回复
热议问题