Skip to content
Advertisement

How to keep n characters of each row of a pd df, where n differs by row?

I have created a df one column of which contains string values that I want to trim based on a different int value each time. Ex.: From:

length String
-3 adcdef
-5 ghijkl

I wanna get:

length String
-3 def
-5 hijkl

What I tried is the following:

JavaScript

However, I keep getting this warning:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

Any ideas on how I can avoid getting it?

Thanks!

Advertisement

Answer

Try with apply:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement