Установка имени осей в Pandas DataFrame

Опубликовано: 28 Марта, 2022

Есть несколько операций, которые можно выполнять над бывшими в Pandas. Давайте посмотрим, как работать с индексом строки и столбца на примерах.

Для ссылки на CSV-файл, используемый в коде, щелкните здесь.

Сбросить имя индекса строки.

Code #1 : We can reset the name of the DataFrame index by using df.index.name attribute.

# importing pandas as pd
import pandas as pd
  
# read the csv file and create DataFrame
df = pd.read_csv("nba.csv")
  
# Visualize the dataframe
print(df)

Output :

# set the index name
df.index.name = "Index_name"
  
# Print the DataFrame
print(df)

Выход :

Code #2 : We can reset the name of the DataFrame index by using df.rename_axis() function.

# importing pandas as pd
import pandas as pd
  
# read the csv file and create DataFrame
df = pd.read_csv("nba.csv")
  
# reset the index name
df.rename_axis("Index_name", axis = "rows")
  
# Print the DataFrame
print(df)

Выход :

Сбросить имя осей столбца

Code #1 : We can reset the name of the DataFrame column axes by using df.rename_axis() function.

# importing pandas as pd
import pandas as pd
  
# read the csv file and create DataFrame
df = pd.read_csv("nba.csv")
  
# Visualize the dataframe
print(df)

Выход :

As we can see in the output, column axes of the df DataFrame does not have any name. So, we will set the name using df.rename_axis() function.

# set the name of column axes
df.rename_axis("Column_Index_name", axis = "columns")
  
# Print the DataFrame
print(df)

Выход :

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.