I have a dataframe signifying the temperature for three days for different regions in India. It's given in the following image. original_dataframe
I need to generate another column in the same dataframe that concatenates the string values of the state and the city which is seen in 'Col5' as per the below image.
I'm aware of merging the values based on columns. But can something like be achieved using pandas or udf in python. Request your insights.
So something like this:
import pandas as pd
import numpy as np
df["Col 5"] = np.nan
df.loc[df['Col 2'].str.contains("/").fillna(False), "Col 5"] = df["Col 1"]
df["Col 5"] = df["Col 5"].ffill() + "|" + df['Col 1']
df.loc[df["Col 5"].str.split("|").str[0] == df["Col 5"].str.split("|").str[1], "Col 5"] = df["Col 5"].str.split("|").str[0]
Is that what you had in mind?