Установка имени осей в Pandas DataFrame
Есть несколько операций, которые можно выполнять над бывшими в Pandas. Давайте посмотрим, как работать с индексом строки и столбца на примерах.
Для ссылки на CSV-файл, используемый в коде, щелкните здесь.
Сбросить имя индекса строки.
Code #1 : We can reset the name of the DataFrame index by using df.index.name attribute.
# importing pandas as pdimport pandas as pd # read the csv file and create DataFramedf = pd.read_csv("nba.csv") # Visualize the dataframeprint(df) |
Output :
# set the index namedf.index.name = "Index_name" # Print the DataFrameprint(df) |
Выход :
Code #2 : We can reset the name of the DataFrame index by using df.rename_axis() function.
# importing pandas as pdimport pandas as pd # read the csv file and create DataFramedf = pd.read_csv("nba.csv") # reset the index namedf.rename_axis("Index_name", axis = "rows") # Print the DataFrameprint(df) |
Выход :
Сбросить имя осей столбца
Code #1 : We can reset the name of the DataFrame column axes by using df.rename_axis() function.
# importing pandas as pdimport pandas as pd # read the csv file and create DataFramedf = pd.read_csv("nba.csv") # Visualize the dataframeprint(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 axesdf.rename_axis("Column_Index_name", axis = "columns") # Print the DataFrameprint(df) |
Выход :
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.