I'm asking you a string separation question in Python dataframe.

Asked 2 years ago, Updated 2 years ago, 78 views

I want to know how to separate strings from Python data frames.

You can use the split method in the list, but I don't know in the dataframe.

Specifically, I would like to leave only a part of the string stored in the data frame as below.

'Hi Seoul' -> 'Hi'

python pandas dataframe string

2022-09-21 21:48

1 Answers

Look at the name column.

data = {'name': ['aaaa 1111', 'bbbb 2222', 'cccc 3333', 'dddd 4444', 'eeee 55555'],
        'year': [2012, 2012, 2013, 2014, 2014],
        'reports': [4, 24, 31, 2, 3],
        'coverage': [25, 94, 57, 62, 70]}



df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])

df
Out[66]: 
            coverage        name  reports  year
Cochice           25   aaaa 1111        4  2012
Pima              94   bbbb 2222       24  2012
Santa Cruz        57   cccc 3333       31  2013
Maricopa          62   dddd 4444        2  2014
Yuma              70  eeee 55555        3  2014

df['name'] = df['name'].apply(lambda e: e.split()[0])

df
Out[68]: 
            coverage  name  reports  year
Cochice           25  aaaa        4  2012
Pima              94  bbbb       24  2012
Santa Cruz        57  cccc       31  2013
Maricopa          62  dddd        2  2014
Yuma              70  eeee        3  2014


2022-09-21 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.