python - How to remove everything after the last occurence of a character in a Dataframe? -


i have dataframe df looks (this sample):

    eq1                    eq2                       eq3 0   apple.fruit            oranage.eatable.fruit     nan 1   pear.eatable.fruit     banana.fruit              nan 2   orange.fruit           tomato.eatable            potato.eatable.vegetable 3   kiwi.eatable           pear.fruit                cabbage.vegetable <and on.. large dataframe> 

i remove after last occurrence of dot . in every element of df , save under different name,say df_temp.
desired ouput:

   eq1               eq2                 eq3 0   apple            oranage.eatable     nan 1   pear.eatable     banana              nan 2   orange           tomato              potato.eatable 3   kiwi             pear                cabbage <and on> 

this tried: df_temp=".".join(df.split(".")[:-1]).
unfortunately seems work strings , not dataframe. have tweak line bit achieve want? please help!

you do:

df_temp = df.apply(lambda x: x.str.split('.').str[:-1].str.join('.')) 

output:

            eq1              eq2             eq3 0         apple  oranage.eatable             nan 1  pear.eatable           banana             nan 2        orange           tomato  potato.eatable 3          kiwi             pear         cabbage    

see string method docs


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -