Python Pandas New Column Based On Another Column
i have a pandas data frame df.drop which has two date columns Joined Date and Terminated Date. i want to get the difference(in days) between terminated date and joined date. but th
Solution 1:
Your code is a bit long. A better way to do it is
df['TerminatedDate'].replace({'Not_Terminated':today}, inplace=True)
If you don't want replace the old column, you could save it to new column.
df['new_col'] = df['TerminatedDate'].replace({'Not_Terminated':today})
The problem with your code is this part else df_drop['TerminatedDate'] for x
as it replaces the a cell by the entire column. It should be else x for x
.
If you want to get the difference in one single action, you would have to create a custom function and apply it row wise.
def get_dif(start,end):
if end == "Not_Terminated":
end = today
return end-start
df['new_col'] = df.apply(lambda df: get_dif(df['JoinedDate'],df['TerminatedDate'], axis=1)
Post a Comment for "Python Pandas New Column Based On Another Column"