Read specific columns with pandas or other python module

前端 未结 3 401
囚心锁ツ
囚心锁ツ 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: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'

提交回复
热议问题