for each column in a pandas dataframe, take the first value that's not nan in python
You can use the df.apply method to apply a custom function to each column, which takes the first value that is not NaN for that column. Here is an example:
main.py
import pandas as pd
import numpy as np
# Create example dataframedf = pd.DataFrame({
'col1': [np.nan, np.nan, 1, 2],
'col2': [3, np.nan, np.nan, 4],
'col3': [np.nan, 5, np.nan, np.nan]
})
# Define custom function to get first non-NaN valuedeffirst_non_nan(col):return col.loc[~col.isna()].iloc[0]
# Apply function to each columnfirst_vals = df.apply(first_non_nan)
# Output resultsprint(first_vals)