How to apply split string to entire data set of excel in pandas

I have a huge data sets, that I would like to apply this

getNumber = df['Permissions','Per'].str.split('-').str[1]`

on the entire dataset and not just on ‘Permissions‘ column, is there any way I could apply it on the entire dataset, instead of specifying it on every column. I am stuck on how to apply this operation to the entire data set and would appreciate any help

in response to Tim’s comment,

my dataset is like this,

dataset in excel sheet:-

enter image description here

I would like to extract the numbers in every columns, so that the new dataset will be having just the numbers.

Answer

You can use pandas.DataFrame.apply:

#setup
df = pd.DataFrame({'col1': ['aaa-0.1', 'bbb-1.2'], 'col2': ['zzzz-0.8', 'abc-9.1']})

#      col1      col2
#0  aaa-0.1  zzzz-0.8
#1  bbb-1.2   abc-9.1

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

#  col1 col2
#0  0.1  0.8
#1  1.2  9.1


As @jezrael say in the comments, you can also convert to float. Simply add .astype(float):

df.apply(lambda x: x.str.split('-').str[1]).astype(float)

Leave a Reply

Your email address will not be published. Required fields are marked *