Read specific columns with pandas or other python module

前端 未结 3 387
囚心锁ツ
囚心锁ツ 2020-11-29 20:25

I have a csv file from this webpage. I want to read some of the columns in the downloaded file (the csv version can be downloaded in the upper right corner).

Let\'s

相关标签:
3条回答
  • 2020-11-29 20:56

    According to the latest pandas documentation you can read a csv file selecting only the columns which you want to read.

    import pandas as pd
    
    df = pd.read_csv('some_data.csv', usecols = ['col1','col2'], low_memory = False)
    

    Here we use usecols which reads only selected columns in a dataframe.

    We are using low_memory so that we Internally process the file in chunks.

    0 讨论(0)
  • 2020-11-29 20:59

    An easy way to do this is using the pandas library like this.

    import pandas as pd
    fields = ['star_name', 'ra']
    
    df = pd.read_csv('data.csv', skipinitialspace=True, usecols=fields)
    # See the keys
    print df.keys()
    # See content in 'star_name'
    print df.star_name
    

    The problem here was the skipinitialspace which remove the spaces in the header. So ' star_name' becomes 'star_name'

    0 讨论(0)
  • 2020-11-29 20:59

    Got a solution to above problem in a different way where in although i would read entire csv file, but would tweek the display part to show only the content which is desired.

    import pandas as pd
    
    df = pd.read_csv('data.csv', skipinitialspace=True)
    print df[['star_name', 'ra']]
    

    This one could help in some of the scenario's in learning basics and filtering data on the basis of columns in dataframe.

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