Issue
I am using python and have a pandas dataframe imported from a csv. I would like to remove every nth value from each entry in a specific column.
For example:
the dataframe column to transform is called:
"Linestring"
- each entry has a varying float lengths and goes like this:
Linestring(151.420 -33.540, 155.464722 -39.069046, 153.30925678 -33.08364825, 152.0998 -31.8090, 150.539067 -30.57578)
- each entry has a varying float lengths and goes like this:
each entry has varying lengths
I would like to remove say every two elements after each comma giving:
Linestring(151.420 -33.540, 153.30925678 -33.08364825, 150.539067 -30.57578)
Attached/linked is a visual guide of what I am after.
Example problem and outcome
Thanks a lot! :)
Solution
Try this. I hope it'll help.
df['Linestring'] = df.Linestring.apply(lambda x: ','.join(x.split(',')[::2]) if ','.join(x.split(',')[::2])[-1] == ')' else ','.join(x.split(',')[::2]) + ')')
Answered By - Alex
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.