Anubhav
Anubhav

Reputation: 157

Remove a certain data from the values of a column from dataframe in python

1.I have a dataframe with ID containing 'a' and 'b' values as a string :

ID                      occ
[['a1'], ['b1']]         3
[['a2'],['a5'],['b2']]   1
[['a3'],['a6'],['b3']]   6
[['a4'], ['b4']]         2

I want to remove all the 'a' values from ID column and take the rest Expected Result:

ID  occ
b1   3
b2   1
b3   6
b4   2

I tried exploding the ID column but it didn't work.

Upvotes: 0

Views: 155

Answers (1)

Nk03
Nk03

Reputation: 14949

If you've a df like this you can use:

df = pd.DataFrame({'ID': [['a1', 'b1'], ['a2', 'b2'], ['a3', 'b3'], ['a4', 'b4']],'occ': [3, 1, 6, 2]})
df.ID = df.ID.str[1]

If ID column is of string type then you can use regex:

df.ID = df.ID.str.extract(r'(b\d+)')

OUTPUT:

   ID  occ
0  b1    3
1  b2    1
2  b3    6
3  b4    2

Upvotes: 1

Related Questions