Python Pandas : How to skip columns when reading a file?

后端 未结 2 621
暖寄归人
暖寄归人 2021-01-03 22:42

I have table formatted as follow :

foo - bar - 10 2e-5 0.0 some information
quz - baz - 4 1e-2 1 some other description in here

When I open

相关标签:
2条回答
  • 2021-01-03 23:04

    The usecols parameter allows you to select which columns to use:

    a = pd.read_table("file", header=None, sep=" ", usecols=range(8))
    

    However, to accept irregular column counts you need to also use engine='python'.

    0 讨论(0)
  • 2021-01-03 23:15

    If you are using Linux/OS X/Windows Cygwin, you should be able to prepare the file as follows:

    cat your_file |  cut -d' ' -f1,2,3,4,5,6,7 > out.file
    

    Then in Python:

    a = pd.read_table("out.file", header=None, sep=" ")
    

    Example:

    Input:

    foo - bar - 10 2e-5 0.0 some information
    quz - baz - 4 1e-2 1 some other description in here
    

    Output:

    foo - bar - 10 2e-5 0.0
    quz - baz - 4 1e-2 1
    

    You can run this command manually on the command-line, or simply call it from within Python using the subprocess module.

    0 讨论(0)
提交回复
热议问题