Home > Blockchain >  How to get string value from object datatype column in python dataframe which have combination of da
How to get string value from object datatype column in python dataframe which have combination of da

Time:02-08

I want to get row from this dataframe which have string value in column "city".

  | city  | region
0 | 5     | 19
1 | Pakri | 37
2 | 9     | 26
3 | 452   | 59
4 | 66    | 2
5 | 226   | 19

Answer should like the below snippet which contain row that have string value in its first column

  | city  | region
0 | Pakri | 37

CodePudding user response:

You could use a boolean filter using apply to the pandas DataFrame o Series.

consider:

import pandas as pd
import numpy as np

#create a series
s=  pd.Series([5,100,'string'], dtype=object)

#set type to string 
s =  s.astype(str)

# apply
temp = s.apply(lambda x: x.isdigit())


#return
#(0, True) (1, True) (2, False)

#index of the string
index = np.where(temp == False)
    
#select by index 
s.loc[index]

#return
# (2, 'string')
  •  Tags:  
  • Related