Categories
Python Answers

How to get a list from Python Pandas DataFrame column headers?

To get a list from Python Pandas DataFrame column headers, we call list on the data frame.

For instance, we write

list(my_dataframe)

where my_dataframe is a data frame to return a list.

Categories
Python Answers

How to get the row count of a Python Pandas DataFrame?

To get the row count of a Python Pandas DataFrame, we can use various properties.

We can write one of

  • len(df.index)
  • df.shape[0]
  • df[df.columns[0]].count()

to get the row count of a Pandas dataframe.

df is the dataframe.

Categories
Python Answers

How to select multiple columns in a Python Pandas dataframe?

To select multiple columns in a Python Pandas dataframe, we pass in an array of column names to select.

For instance, we write

df1 = df[['a', 'b']]

to select columns 'a' and 'b' and return it.

Categories
Python Answers

How to delete a column from a Python Pandas DataFrame?

To delete a column from a Python Pandas DataFrame, we call the drop method.

For instance, we write

df = df.drop('column_name', 1)

to call df.drop to drop the column_name column only.

The 1 indicates we drop only the column in the 1st argument.

Categories
Python Answers

How to rename column names in Python Pandas?

To rename column names in Python Pandas, we call the dataframe rename method.

For instance, we write

df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})

to rename the oldName1 and oldName2 columns to newName1 and newName2 with df.rename.

And then we assign the returned dataframe back to df.