How to insert space in Pandas Column String

pandas insert multiple columns
pandas pad column with zeros
how to add space between words in python
pandas dataframe column padding
pandas to string
how to add spaces in python string
pandas pad dataframe
pandas series insert

This column has bad date/time information in it:

import pandas as pd
df = pd.DataFrame({
    'date': ['1/25/201612:00:00AM','2/25/201712:00:00AM','3/25/201812:00:00AM',
             '4/25/201912:00:00AM','5/25/201912:00:00AM','6/25/201912:00:00AM']})

I tried this function below, however it produces a column of NaNs:

def insert_space(string, integer):
    return string[0:integer] + ' ' + string[integer:]
insert_space(df['date'], 9)

Desired Output example (any date format works!):

    date
0   1/25/2016 12:00:00AM
1   2/25/2017 12:00:00AM
2   3/25/2018 12:00:00AM
3   4/25/2019 12:00:00AM
4   5/25/2019 12:00:00AM
5   6/25/2019 12:00:00AM

Or

    date
0   1/25/2016
1   2/25/2017 
2   3/25/2018 
3   4/25/2019 
4   5/25/2019 
5   6/25/2019

Applying a function to every row of a specified column works like this:

df['date'].apply(lambda x: insert_space(x, 9), axis=1)

Note that, if working with datetime objects, you'll need to modify the function accordingly. A datetime object, such as datetime.time(), is not subscriptable and will raise a TypeError if you try and run it through your insert_space function. str(datetime.time()) will return a string, however.

Python, Pandas provide a method to add padding (whitespaces or other Type: Series with added spaces/characters on the passed side of string. Add spaces in python with example : In this Tutorial we will learn how to add Spaces (leading, Trailing Spaces to string) in python for following Scenarios. we will also be looking at adding leading and trailing space of the column in pandas python. Add Space at the start of the string in Python – rjust() function in python

how about something like this:

df['date'] = pd.to_datetime(df['date'], format="%m/%d/%Y%I:%M:%S%p")

you can find the explanation for the format here: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

pandas.Series.str.pad — pandas 1.1.0 documentation, Pad strings in the Series/Index up to width. Parameters. widthint. Minimum width of resulting string; additional characters will be filled with character defined in� Often in a pandas dataframe we have columns that contain string values. Luckily, pandas provides an easy way of applying string methods to whole columns which are just pandas series objects. We can access the values of these series objects (or columns) as strings and apply string methods to them by using the str attribute of the series.

As it stands, the function you provide just returns a value, which is immediately trashed.

Here is a solution using basic for loop (can be trivially converted to list comprehension or functionalized).

import pandas as pd

# First format
df = pd.DataFrame({
    'date': ['1/25/201612:00:00AM','2/25/201712:00:00AM','3/25/201812:00:00AM',
             '4/25/201912:00:00AM','5/25/201912:00:00AM','6/25/201912:00:00AM']})

for i in range(len(df)):
    df['date'][i] = df['date'][i][:-10] + " " + df['date'][i][-10:]

print(df)
#                     date
# 0  1/25/2016  12:00:00AM
# 1  2/25/2017  12:00:00AM
# 2  3/25/2018  12:00:00AM
# 3  4/25/2019  12:00:00AM
# 4  5/25/2019  12:00:00AM
# 5  6/25/2019  12:00:00AM

# Second format
df = pd.DataFrame({
    'date': ['1/25/201612:00:00AM','2/25/201712:00:00AM','3/25/201812:00:00AM',
             '4/25/201912:00:00AM','5/25/201912:00:00AM','6/25/201912:00:00AM']})

for i in range(len(df)):
    df['date'][i] = df['date'][i][:-10]

print(df)

#         date
# 0  1/25/2016
# 1  2/25/2017
# 2  3/25/2018
# 3  4/25/2019
# 4  5/25/2019
# 5  6/25/2019

UPDATE: Below are list comprehensions for the respective calls that should be far more efficient:

df['date'] = [v[:-10] + " " + v[-10:] for v in df['date']]
df['date'] = [v[:-10] for v in df['date']]

pandas.DataFrame.insert — pandas 1.1.0 documentation, Series and Index are equipped with a set of string processing methods that make it For instance, you may have columns with leading or trailing whitespace: in comparison to Series of type string (e.g. you can't add strings to each other: s +� Add new column to DataFrame in Pandas using assign() Let’s add a column ‘Marks’ i.e. mod_fd = df_obj.assign( Marks=[10, 20, 45, 33, 22, 11]) mod_fd. It will return a new dataframe with a new column ‘Marks’ in that Dataframe. Values provided in list will used as column values. Contents of new dataframe mod_fd are,

Working with text data — pandas 1.1.0 documentation, Series and Index are equipped with a set of string processing methods that make it easy to operate on For instance, you may have columns with leading or trailing whitespace: pad(), Add whitespace to left, right, or both sides of strings. Let’s see how to split a text column into two columns in Pandas DataFrame. Method #1 : Using Series.str.split() functions. Split Name column into two different columns. By default splitting is done on the basis of single space by str.split() function.

Working with Text Data — pandas 0.17.0 documentation, String or regular expression to split on. If not specified, split on whitespace. nint, default -1 (all). Limit number of splits in output. str.strip() function is used to remove or strip the leading and trailing space of the column in pandas dataframe. Str.replace() function is used to strip all the spaces of the column in pandas Let’s see an Example how to trim or strip leading and trailing space of column and trim all the spaces of column in a pandas dataframe using lstrip() , rstrip() and strip() functions .

pandas.Series.str.split — pandas 1.1.0 documentation, we will also be looking at adding leading and trailing space of the column in pandas python. Add Space at the start of the string in Python – rjust() function in� Let’s discuss how to add new columns to existing DataFrame in Pandas. There are multiple ways we can do this task. Method #1: By declaring a new list as a column.

Comments
  • Can you elaborate on what you mean by "it doesnt like to work on Pandas DataFrames". Do you get an error or is the output not what you want? What output do you get?
  • Hi, it produces a column of NaNs
  • Cool. Also, how are you passing in the values from your dataframe column into the function? In cell 1 you've called your dataframe df with column date. In cell 2, you call the function with prod['PRODUCTIONDATE']
  • This was a Type-o