How to remove colon in python

I have a column named "Start Time" is it a string and it displays the time as 11:20:15. I want to have the time in the format 112015. So I need to replace the colon.

I have tried:

["Start Time"] = df["Start Time"].replace(":","", regex=True)

But that didn't work. It didn't give an error either but the time remains 11:20:15.

Can someone point me in the right direction?

asked Jan 13, 2020 at 9:30

How to remove colon in python

3

You need to add .str to handle each value as a str and apply .replace() (documentation) :

df["Start Time"] = df["Start Time"].str.replace(":","")

answered Jan 13, 2020 at 9:32

How to remove colon in python

F BlanchetF Blanchet

1,3003 gold badges18 silver badges31 bronze badges

Is the df["Start Time"] column a string datatype? Often pandas will read in dates as datetime objects. You can check this by printing df.dtypes and see if it says object for this column.

Then I'd recommend using df.strftime (string formatter) as done here: How to change the datetime format in pandas if you really want the string in the format you mentioned.

answered Jan 13, 2020 at 9:37

How to remove colon in python

LourensLourens

1041 silver badge10 bronze badges

3

Looks like you have a datatime object in Start Time column. Use dt.strftime(%H%M%S)

Ex:

df["Start Time"].dt.strftime(%H%M%S)

answered Jan 13, 2020 at 9:57

How to remove colon in python

RakeshRakesh

79.5k17 gold badges70 silver badges107 bronze badges

When I did:

df['Start Time'] = df['Start Time'].astype(str)

And then:

["Start Time"] = df["Start Time"].replace(":","", regex=True)

It worked perfectly. Thank you all for thinking with me!

answered Jan 18, 2020 at 17:39

How to remove colon in python

Sj03rdSj03rd

511 silver badge10 bronze badges

Sometimes we want to remove all occurrences of a character from a string. There are two common ways to achieve this.

Python Remove Character from String

  1. Using string replace() function
  2. Using string translate() function

Python Remove Character from String using replace()

We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string. Note that the string is immutable in Python, so this function will return a new string and the original string will remain unchanged.

s = 'abc12321cba'

print(s.replace('a', ''))

Output: bc12321cb

Python Remove Character from String using translate()

Python string translate() function replace each character in the string using the given translation table. We have to specify the Unicode code point for the character and ‘None’ as a replacement to remove it from the result string. We can use ord() function to get the Unicode code point of a character.

s = 'abc12321cba'

print(s.translate({ord('a'): None}))

Output: bc12321cb If you want to replace multiple characters, that can be done easily using an iterator. Let’s see how to remove characters ‘a’, ‘b’ and ‘c’ from a string.

s = 'abc12321cba'

print(s.translate({ord(i): None for i in 'abc'}))

Output: 12321

Removing Spaces from a String

s = ' 1 2 3 4 '
print(s.replace(' ', ''))  # 1234
print(s.translate({ord(i): None for i in ' '}))  # 1234

Python Remove newline from String

s = 'ab\ncd\nef'
print(s.replace('\n', ''))
print(s.translate({ord('\n'): None}))

Remove substring from string

String replace() function arguments is string. Let’s see how to remove a word from a string.

s = 'ab12abc34ba'
print(s.replace('ab', ''))

Output: 12c34ba

Remove specified number of times

We can also pass a third parameter in replace() function to specify the number of times replacement should be performed.

s = 'abababab'
print(s.replace('a', 'A', 2))

Output: AbAbabab

You can checkout complete python script and more Python examples from our GitHub Repository.

How do you remove a colon from a string in Python?

You can remove a character from a Python string using replace() or translate(). Both these methods replace a character or string with a given value. If an empty string is specified, the character or string you select is removed from the string without a replacement.

How do I remove a symbol from a string in Python?

Removing symbol from string using replace() One can use str. replace() inside a loop to check for a bad_char and then replace it with the empty string hence removing it.

How do you slice a colon in Python?

The basic syntax for a slice is square brackets with colons and integers inside "[0:1:2]". The slice operator has optional arguments for if you decide to leave things blank. It will automatically start at index 0, stop at the end and step by 1 unless you declare otherwise.

Is colon required in Python?

In Python, a colon is required at the beginning of every block of code. It is easier to explain with an example. Notice how at the end of the if statement I have a colon. This tells Python that the next line of indented code should only be run IF the condition is true.