how to create a value in a column if the rows of the column have no spaces, and want to be spaced based on the value of the main column

Viewed 37

I have a dataframe with no spaces in the column like this:

for example

time          title
10:30         BridgeToTerabithia    
11:30         ManOnALedge
12:30         Memento

and I have the metadata for the actual movie title

for example

         title
        Bridge To Terabithia    
        Man On A Ledge
        Memento

how to be able to match the data in dataframe a with the metadata, so that the data title has the actual value according to the metadata thanks for the answer

2 Answers

For match remove spaces:

df2['title'] = df2['title'].str.replace('\s+','')

Or add spaces after uppercase:

df1['title'] = df1['title'].replace('(?!^)(?=[A-Z])',' ',regex=True)
print (df)

    time                 title
0  10:30  Bridge To Terabithia
1  11:30        Man On A Ledge
2  12:30               Memento

Last use DataFrame.merge:

df = df1.merge(df2, on='title')

Since you haven't given any code on what you have tried, I will give a way to get you started with solving this problem: Remove spaces from the actual title and use that to find the title in the dataframe you want to fix (use a df.merge to join between two dfs and merge on the title without the space)

Related