0

I have a table index looks like that:

k__Bacteria;p__Spirochaetes
k__Bacteria;p__Acidobacteria
k__Bacteria;p__Actinobacteria
k__Bacteria;p__Armatimonadetes
...........

I want to delete every word that come before the "" ('k_Bacteria;p') So that I have only the words "Spirochaetes", "Actinobacteria" and so forth.

how can I do it?

TNX!

Rotem Bartuv
  • 133
  • 7
  • 2
    Does this answer your question? [Split pandas column and add last element to a new column](https://stackoverflow.com/questions/38498718/split-pandas-column-and-add-last-element-to-a-new-column) – MrNobody33 Aug 11 '20 at 07:23

1 Answers1

1

If need working with index values use str.split and select last values by indexing [-1]:

print (df)
                                col
k__Bacteria;p__Spirochaetes       1
k__Bacteria;p__Acidobacteria      2
k__Bacteria;p__Actinobacteria     3
k__Bacteria;p__Armatimonadetes    4

df.index = df.index.str.split('_').str[-1]
print (df)
                 col
Spirochaetes       1
Acidobacteria      2
Actinobacteria     3
Armatimonadetes    4

If need working with column col use:

print (df)
                              col
0     k__Bacteria;p__Spirochaetes
1    k__Bacteria;p__Acidobacteria
2   k__Bacteria;p__Actinobacteria
3  k__Bacteria;p__Armatimonadetes

df.col = df.col.str.split('_').str[-1]
print (df)
0     Spirochaetes
1    Acidobacteria
2   Actinobacteria
3  Armatimonadetes
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252