Pandas read_csv dtype read all columns but few as string

前端 未结 1 683
日久生厌
日久生厌 2020-12-08 06:13

I\'m using Pandas to read a bunch of CSVs. Passing an options json to dtype parameter to tell pandas which columns to read as string instead of the default:

         


        
相关标签:
1条回答
  • 2020-12-08 06:56

    EDIT - sorry, I misread your question. Updated my answer.

    You can read the entire csv as strings then convert your desired columns to other types afterwards like this:

    df = pd.read_csv('/path/to/file.csv', dtype=str)
    # example df; yours will be from pd.read_csv() above
    df = pd.DataFrame({'A': ['1', '3', '5'], 'B': ['2', '4', '6'], 'C': ['x', 'y', 'z']})
    types_dict = {'A': int, 'B': float}
    for col, col_type in types_dict.items():
        df[col] = df[col].astype(col_type)
    

    Another approach, if you really want to specify the proper types for all columns when reading the file in and not change them after: read in just the column names (no rows), then use those to fill in which columns should be strings

    col_names = pd.read_csv('file.csv', nrows=0).columns
    types_dict = {'A': int, 'B': float}
    types_dict.update({col: str for col in col_names if col not in types_dict})
    pd.read_csv('file.csv', dtype=types_dict)
    
    0 讨论(0)
提交回复
热议问题