'Create python function from script
Struggling to get this code to work as a function. It needs to separate values within a particular field which contains + symbols and create a new row which has all the same data as the original.
data = {"GUI":[1005],"HAB_TYPE":["A5.37+A5.37"]}
surveyMaps = pd.DataFrame(data)
After running the function the output should look something like:
surveyMapsOutput = {"GUI":[1005,1005],"HAB_TYPE":["A5.37","A5.37"]}
It works when it exists in the script like so:
surveyMapsOutput = surveyMaps.drop('HAB_TYPE', axis=1) \
.join(surveyMaps['HAB_TYPE']
.str
.split('+', expand=True)
.stack()
.reset_index(level=1, drop=True).rename('HAB_TYPE')) \
.reset_index(drop=True)
This is my attempt at creating a function to do the same job:
def split_mosaic(input_data):
"""function which splits mosaic codes into individual habitats codes """
survey_output = input_data
survey_output.drop('HAB_TYPE', axis=1) \
.join(survey_output['HAB_TYPE']
.str
.split('+', expand=True)
.stack()
.reset_index(level=1, drop=True).rename('HAB_TYPE')) \
.reset_index(drop=True)
return survey_output
when trying to call the function I do
surveyMapOutput= split_mosaic(surveysMaps)
However this does not remove the + sign as it does with the first example of the code. Any advice?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
