Difficulty importing .dat file

前端 未结 1 1996
野性不改
野性不改 2020-12-15 09:11

I am somehow having difficulty reading in this file into python with pandas read_table function. http://www.ssc.wisc.edu/~bhansen/econometrics/invest.dat

This is my

相关标签:
1条回答
  • 2020-12-15 09:40

    Dont know about read_table, but you can read this file directly as follows:

    import pandas as pd    
    
    with open('/tmp/invest.dat','r') as f:
        next(f) # skip first row
        df = pd.DataFrame(l.rstrip().split() for l in f)
    
    print(df)
    

    Prints:

                  0            1             2            3
    0     17.749000   0.66007000    0.15122000   0.33150000
    1     3.9480000   0.52889000    0.11523000   0.56233000
    2     14.810000    3.7480300    0.57099000   0.12111000
    ...
    ...
    

    The same can be obtained as follows:

    df = pd.read_csv('/tmp/invest.dat', sep='\s+', header=None, skiprows=1)
    
    0 讨论(0)
提交回复
热议问题