Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # date: 2025.02.22
- # author: furas
- # stackoverflow: [pandas - python definition that use try and except loop to create a column using the apply funtion - Stack Overflow](https://stackoverflow.com/questions/79446546/python-definition-that-use-try-and-except-loop-to-create-a-column-using-the-appl)
- # doc: [pandas.DataFrame.apply — pandas 2.2.3 documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html)
- # --- version 1 - without `apply` ---
- def date_difference(dataframe):
- dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].astype(str)
- dataframe['Issued Date'] = dataframe['Issued Date'].astype(str)
- dataframe['Issued Date'] = dataframe['Issued Date'].fillna(0)
- dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].fillna(0)
- results = []
- for x, y in zip(dataframe['Issued Date'], dataframe['Permit Creation Date']):
- try:
- d1 = datetime.datetime.strptime(x, "%Y-%m-%d")
- d2 = datetime.datetime.strptime(y, "%Y-%m-%d")
- print(abs(d1-d2).days)
- results.append(abs(d1-d2).days)
- except Exception as e:
- print(e.args)
- results.append(None)
- return results
- type1['Date difference'] = date_difference(type1) # without `apply`
- # --- version 2 - without `apply` - some modifications outside function ---
- dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].astype(str)
- dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].fillna(0)
- dataframe['Issued Date'] = dataframe['Issued Date'].astype(str)
- dataframe['Issued Date'] = dataframe['Issued Date'].fillna(0)
- def date_difference(dataframe):
- results = []
- for x, y in zip(dataframe['Issued Date'], dataframe['Permit Creation Date']):
- try:
- d1 = datetime.datetime.strptime(x, "%Y-%m-%d")
- d2 = datetime.datetime.strptime(y, "%Y-%m-%d")
- print(abs(d1-d2).days)
- results.append(abs(d1-d2).days)
- except Exception as e:
- print(e.args)
- results.append(None)
- return results
- type1['Date difference'] = date_difference(type1) # without `apply`
- # --- version 3 - without `apply` - some modifications outside function - use `iterrows()` instead of `zip()` ---
- dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].astype(str)
- dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].fillna(0)
- dataframe['Issued Date'] = dataframe['Issued Date'].astype(str)
- dataframe['Issued Date'] = dataframe['Issued Date'].fillna(0)
- def date_difference(dataframe):
- results = []
- for index, row in dataframe.iterrows(): # use `iterrows()`
- try:
- d1 = datetime.datetime.strptime(row['Issued Date'], "%Y-%m-%d")
- d2 = datetime.datetime.strptime(row['Permit Creation Date'], "%Y-%m-%d")
- print(abs(d1-d2).days)
- results.append(abs(d1-d2).days)
- except Exception as e:
- print(e.args)
- results.append(None)
- return results
- type1['Date difference'] = date_difference(type1) # without `apply`
- # --- version 4 - apply ---
- dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].astype(str)
- dataframe['Permit Creation Date'] = dataframe['Permit Creation Date'].fillna(0)
- dataframe['Issued Date'] = dataframe['Issued Date'].astype(str)
- dataframe['Issued Date'] = dataframe['Issued Date'].fillna(0)
- def apply_date_difference(row): # it gets single row (Series)
- try:
- d1 = datetime.datetime.strptime(row['Issued Date'], "%Y-%m-%d")
- d2 = datetime.datetime.strptime(row['Permit Creation Date'], "%Y-%m-%d")
- print(abs(d1-d2).days)
- return abs(d1-d2).days
- except Exception as e:
- print(e.args)
- return None
- type1['Date difference'] = type1.apply(date_difference)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement