Let's say I have a dataframe that has, amnog others, two columns, date1, and date2. Date1 is null when date2 is not, and vice-versa.
I want to create a column named "date".
I did this:
def setDate(s1, s2):
if not isinstance(s1, str):
return s2
else:
return s1
Then, I want to do something like:
data["DATE"] = data[["DATE1", "DATE2"]].apply(lambda x,y : setDate(x,y))
but I keep getting:
TypeError: ("<lambda>() missing 1 required positional argument: 'y'", 'occurred at index DATE1')
How do I use apply on a dataframe to do what I want?
try:
data['DATE'] = pd.to_datetime(data.DATE1.fillna(data.DATE2))
or if you want to keep the dtypes the way they are
data['DATE'] = data.DATE1.fillna(data.DATE2)
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.