MS python学习(18)

学习Pandas.DataFrame(2)

load csv(comma seperated variable) files to DataFrame and vice versa

upload csv files
read/write csv files

  1. load data into jupyter notebook, create a new folder and then upload the csv files into it. (CSV = comma seperated variable) 文件格式如下:

    Name,City,Country    # first line is column names
    Seattle-Tacoma,Seattle,USA
    Dulles,Washington,USA
    Heathrow,London,United Kingdom
    Schiphol,Amsterdam,Netherlands
    Changi,Singapore,Singapore
    Pearson,Toronto,Canada
    Narita,Tokyo,Japan
    

    MS python学习(18)_第1张图片

  2. Normal csv files:

    dataframe = pd.read_csv(‘Folder/name.csv’)

    MS python学习(18)_第2张图片

  3. csv files had bad lines:

    dataframe = pd.read_csv(‘Folder/name.csv’, on_bad_lines='skip')

    注意error_bad_lines=False 这个参数在pandas 1.3后就不用了

    第四行多了一个comma,直接读取会报错,加了on_bad_lines='skip'

    后这一行就不会读取,另外如果有空值的地方会直接显示为NaN
    MS python学习(18)_第3张图片

  4. csv fiels do not have column headers:

    dataframe = pd.read_csv(‘Folder/name.csv’, header=None) (column name变为0,1,2…)

    dataframe = pd.read_csv(‘Folder/name.csv’, header=None,Name=['name1','name2','name3'...])(Name参数指定column name)

MS python学习(18)_第4张图片

MS python学习(18)_第5张图片

  1. 将DataFrame 导出为csv files

    dataframe.to_csv(‘NewName.csv’) – 默认会把行号也导入进去

    dataframe.to_csv(‘NewName.csv’, index=False) – 不导入行号

MS python学习(18)_第6张图片

TO BE CONTINUED…

你可能感兴趣的:(python,python,学习,pandas)