Reputation:
In the following exp:
class Project
0 23454 Math
1 23455 Physics
2 456 Music
3 45 Danse
4 23455 Acting
How can i remove values in which the column values for class has less than 5 elements using the length function? So i will get the following dataframe:
class Project
0 23454 Math
1 23455 Physics
2 Music
3 Danse
4 23455 Acting
Upvotes: 0
Views: 402
Reputation: 24314
use mask()
and astype()
for converting 'class' column to string and then check the length of string by str.len()
if it is less then 5 or not so based on that mask change value:
df['class']=df['class'].mask(df['class'].astype(str).str.len().lt(5),'')
OR
via loc
accessor
df.loc[df['class'].astype(str).str.len().lt(5),'class']=''
Note: similarly you can also use np.where()
or df.where()
output:
class Project
0 23454 Math
1 23455 Physics
2 Music
3 Danse
4 23455 Acting
Upvotes: 1