Home > Software engineering >  How to get a column name to display even when there is no data for that column?
How to get a column name to display even when there is no data for that column?

Time:12-09

I want to get the names of the columns of a pandas dataframe even when I don't have data according to certain search criteria

that's how it is now:

Empty DataFrame
Columns: []
Index: []

as I want it to be:

Empty DataFrame
Columns: [column_name]
Index: []

I select the data directly from the MYSQL DB, I do not save it in memory !

CodePudding user response:

I am not sure of your need, but if you want to have a dataframe with column names, you can initialize it with the column names :

    df = pd.DataFrame(columns=['A', 'B', 'C'])

CodePudding user response:

I'm not entirely sure, what you are asking about. But to get names of all DataFrame columns, simply do:

df.columns
  • Related