Index pandas DataFrame by column numbers, when column names are integers

前端 未结 3 2134
遥遥无期
遥遥无期 2021-02-20 00:14

I am trying to keep just certain columns of a DataFrame, and it works fine when column names are strings:

In [2]: import numpy as np

In [3]: import pandas as pd         


        
3条回答
  •  难免孤独
    2021-02-20 00:49

    Just convert the headers from integer to string. This should be done almost always as a best practice when working with pandas datasets to avoid surprise

    df.columns = df.columns.map(str)
    

提交回复
热议问题