I have a shp with a column NOM_CONCAT
and I can not explode it to have a unique information,
Each time, I have the same error message and I can't find solutions.
#TypeError: explode() takes 1 positional argument but 2 were given
Code:
import pandas as pd
import numpy as np
import time
import datetime
import sys
import geopandas as gpd
gdf = gpd.read_file('RSF_Fiche_Canalisation-power_shp_ME_Position-04012021.shp')
gdf['NOM_CONCAT'] = gdf['NOM_CONCAT'].str.split(', ')
gdf.NOM_CONCAT = gdf.NOM_CONCAT.fillna({i: [] for i in gdf.index})
gdf = gdf.explode('NOM_CONCAT')
print ("temp_complet_explode",gdf)
Current situation:
Situation I hope:
Best Answer
Per the doc:
Source: https://geopandas.org/reference.html
It's a method which applies directly on your
GeoDataFrame
object, therefore any extra argument you pass will be counted as a 2nd argument, hence the error you face. Please notice thatGeoDataFrame.explode()
is intended to:Therefore, the following is working because all geometries are multilines, so it splits them into new rows, each hosting one of the part from the original geometry:
which results in:
And calling
.explode()
will actually explode your geometries:But if you do not have multiple geometries:
calling
.explode()
will return your original dataframe:What you want is probably to use pandas explode method instead, which is waiting for a column parameter:
You can finally convert it back to a GeoDataFrame:
Beware of the index changes after the explosion. You can obviously change it depending on your needs, e.g. with
.reset_index()
.