Home > Back-end >  Is there a way to create a list of values out of existing column value in pandas?
Is there a way to create a list of values out of existing column value in pandas?

Time:05-22

Let's say I have the following pandas dataframe:

sample_df

I would like to create a new column with the value of ColB and -1 * ColB. The resulting dataframe should look like this:

resulting_df

How can I achieve this efficiently?

CodePudding user response:

simple way:

df['ColC'] = df['ColB'].astype(str)   ", "   (df['ColB']*-1).astype(str)

CodePudding user response:

You Can do it like that Direct and clear way

def Test(Col1,Col2):
    for i in range(len(Col1)):
        yield [Col1[i],-1*Col2[i]]

z=Test(data["ColB"],data["ColB"])
data["ColC"]=list(z)
  • Related