Skip to content

Instantly share code, notes, and snippets.

@micycle1
Created July 4, 2023 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micycle1/83922edf5958f689807bcc023b4d8291 to your computer and use it in GitHub Desktop.
Save micycle1/83922edf5958f689807bcc023b4d8291 to your computer and use it in GitHub Desktop.
Function to print the date of the first non-null value for each column in a DataFrame, sorting the output by date.
def print_first_non_null_dates(df):
non_null_dates = {}
for column in df.columns:
non_null_values = df[column].dropna()
if not non_null_values.empty:
first_non_null_date = non_null_values.index[0]
non_null_dates[column] = first_non_null_date
sorted_dates = sorted(non_null_dates.items(), key=lambda x: x[1])
for column, date in sorted_dates:
print(f"'{column}': {date.strftime('%Y-%m-%d')}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment