Created
July 4, 2023 15:20
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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