简体   繁体   中英

Create a new column in a second dataframe by passing column values of a different dataframe and a scalar to a function in Pandas Python?

My dataframe1 contains the day column which has numeric data from 1 to 7 for each day of the week. 1 - Monday, 2 - Tuesday...etc. This day column is the day of Departure of a flight.

I need to create a new column dayOfBooking in a second dataframe2 which finds day of the week based on the number of days before a person books a flight and the day of departure of the flight.

For that I've written this function:

def findDay(dayOfDeparture, beforeDay):
    beforeDay = int(beforeDay)
    beforeDay = beforeDay % 7
    if((dayOfDeparture - beforeDay) > 0):
        dayAns = currDay - beforeDay;
    else:
        dayAns = 7 - abs(dayOfDeparture - beforeDay)
return(dayAns)

I want something like:

dataframe2["dayOfBooking"] = findDay(dataframe1["day"], i)

where i is the scalar value.

I can see that findDay takes the entire column day of dataframe1 instead of taking a single value for each row.

Is there an easy way to accomplish this like when we want a third column to be the sum of two other columns for each row, we can just write this:

dataframe["sum"] = dataframe2["val1"] + dataframe2["val2"]

EDIT: Figured it out. Answer and explanation below.

df2["colname"] = df.apply(lambda row: findDay(row['col'], i), axis = 1)

We have to use the apply function if we want to extract each row value of a particular column and pass it to a user defined function.

axis = 1 denotes that every row value is being taken for that column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM