Neil
Neil

Reputation: 8247

how to replace string at specific index in pandas dataframe

I have following dataframe in pandas

    code     bucket
    0        08:30:00-9:00:00
    1        10:00:00-11:00:00
    2        12:00:00-13:00:00

I want to replace 7th character 0 with 1, my desired dataframe is

   code     bucket
    0       08:30:01-9:00:00
    1       10:00:01-11:00:00
    2       12:00:01-13:00:00

How to do it in pandas?

Upvotes: 3

Views: 2930

Answers (2)

jpp
jpp

Reputation: 164843

Avoid string operations where possible

You lose a considerable amount of functionality by working with strings only. While this may be a one-off operation, you will find that repeated string manipulations will quickly become expensive in terms of time and memory efficiency.

Use pd.to_datetime instead

You can add additional series to your dataframe with datetime objects. Below is an example which, in addition, creates an object dtype series in the format you desire.

# split by '-' into 2 series
dfs = df.pop('bucket').str.split('-', expand=True)

# convert to datetime
dfs = dfs.apply(pd.to_datetime, axis=1)

# add 1s to first series
dfs[0] = dfs[0] + pd.Timedelta(seconds=1)

# create object series from 2 times
form = '%H:%M:%S'
dfs[2] = dfs[0].dt.strftime(form) + '-' + dfs[1].dt.strftime(form)

# join to original dataframe
res = df.join(dfs)

print(res)

   code                   0                   1                  2
0     0 2018-10-02 08:30:01 2018-10-02 09:00:00  08:30:01-09:00:00
1     1 2018-10-02 10:00:01 2018-10-02 11:00:00  10:00:01-11:00:00
2     2 2018-10-02 12:00:01 2018-10-02 13:00:00  12:00:01-13:00:00

Upvotes: 0

jezrael
jezrael

Reputation: 863801

Use indexing with str:

df['bucket'] = df['bucket'].str[:7] + '1' + df['bucket'].str[8:]

Or list comprehension:

df['bucket'] = [x[:7] + '1' + x[8:] for x in df['bucket']]

print (df)
   code             bucket
0     0   08:30:01-9:00:00
1     1  10:00:01-11:00:00
2     2  12:00:01-13:00:00

Upvotes: 1

Related Questions