Переименование столбцов в GeoPandas: подробное руководство с примерами кода

Чтобы переименовать столбец в GeoPandas, вы можете использовать различные методы. Вот несколько примеров вместе с соответствующим кодом:

Метод 1: использование функции rename()

import geopandas as gpd
# Read the GeoDataFrame from a file
gdf = gpd.read_file('your_file.geojson')
# Rename a single column
gdf = gdf.rename(columns={'old_column_name': 'new_column_name'})
# Rename multiple columns
gdf = gdf.rename(columns={'old_column_name1': 'new_column_name1', 'old_column_name2': 'new_column_name2'})

Метод 2. Непосредственное присвоение имен новым столбцам

import geopandas as gpd
# Read the GeoDataFrame from a file
gdf = gpd.read_file('your_file.geojson')
# Assign new column names to the 'columns' attribute
gdf.columns = ['new_column_name1', 'new_column_name2', 'new_column_name3']

Метод 3: использование функции set_axis()

import geopandas as gpd
# Read the GeoDataFrame from a file
gdf = gpd.read_file('your_file.geojson')
# Set new column names using the 'set_axis()' function
gdf.set_axis(['new_column_name1', 'new_column_name2', 'new_column_name3'], axis=1, inplace=True)

Метод 4: использование метода rename()базового DataFrame pandas

import geopandas as gpd
# Read the GeoDataFrame from a file
gdf = gpd.read_file('your_file.geojson')
# Use the 'rename()' method of the underlying pandas DataFrame
gdf = gdf.rename(columns={'old_column_name': 'new_column_name'})