Home > front end >  Removing blank spaces from the beginining all rows for the object in the string
Removing blank spaces from the beginining all rows for the object in the string

Time:03-24

I have a code:

minyr =str(full_df['origin_time'])
print (minyr)

which gives me the result:

0             1840-03-31 06:30:00
1             1841-01-27 21:55:00
2             1841-04-21 15:00:00
3             1841-10-30 00:00:00
4             1842-10-27 19:30:00
                  ...            
438    2022-01-27 11:05:37.331000
439    2022-01-29 16:12:48.710000
440    2022-02-02 16:15:30.665000
441    2022-02-08 05:01:38.648000
442    2022-02-11 01:04:12.126000
Name: origin_time, Length: 12994, dtype: object

I want to remove blank spaces from the beginning of all rows containing them. How can I achieve that? I did try lstrip but did not manage to make it work (I am quite new to python).

Many thanks for any help..

CodePudding user response:

You can select the series, modify it and reassign it to your dataframe.

df['origin_time'] = df['origin_time'].str.lstrip()

CodePudding user response:

I would suggest the following:

text = '0             1840-03-31 06:30:00'
text.replace(" ", "")

that would result in:

'01840-03-3106:30:00'

or

" ".join(text.split())

resulting in

'0 1840-03-31 06:30:00'
  • Related