get list of pandas dataframe columns based on data type

前端 未结 12 1388
后悔当初
后悔当初 2020-12-07 07:37

If I have a dataframe with the following columns:

1. NAME                                     object
2. On_Time                                      object
         


        
12条回答
  •  Happy的楠姐
    2020-12-07 08:00

    I came up with this three liner.

    Essentially, here's what it does:

    1. Fetch the column names and their respective data types.
    2. I am optionally outputting it to a csv.

    inp = pd.read_csv('filename.csv') # read input. Add read_csv arguments as needed
    columns = pd.DataFrame({'column_names': inp.columns, 'datatypes': inp.dtypes})
    columns.to_csv(inp+'columns_list.csv', encoding='utf-8') # encoding is optional
    

    This made my life much easier in trying to generate schemas on the fly. Hope this helps

提交回复
热议问题