I have a sample dataframe on which I am trying to apply based on the column dtype:
df = pd.DataFrame(np.random.randint(0,10,size =(6,2)),columns=["A","B"])
df.loc[2,"B"]=np.NaN
df["C"]=np.NaN
df["st"]=["Mango"]*6
df["date"]=["2001-01-01","2001-01-02","2001-01-03","2001-01-04","2001-01-05","2001-01-06"]
df["date"]=pd.to_datetime(df["date"])
df
Sample dataframe:
A B C fruit date
0 1 1.0 NaN Mango 2001-01-01
1 4 3.0 NaN Mango 2001-01-02
2 8 NaN NaN Mango 2001-01-03
3 2 1.0 NaN Mango 2001-01-04
4 9 6.0 NaN Mango 2001-01-05
5 9 6.0 NaN Mango 2001-01-06
I'm trying to transform the DF based on the column dtypes and generate a single row.
pseudocode:
if data_type(column) == String:
#first value in the column
return column_value[0]
if data_type(column) == datetime:
#last value in the column
return column_value[-1]
if data_type(column) == int or data_type(column) == float:
if all_values_in_column==np.NaN:
return np.NaN
else:
#mean of the column
return mean(column)
Code:
from pandas.api.types import is_datetime64_any_dtype as is_datetime
from pandas.api.types import is_float,is_float_dtype,is_integer,is_integer_dtype
def check(series):
if is_string_dtype(series)==True:
return series[0]
elif is_datetime(series) == True:
return series[len(series)-1]
elif is_integer_dtype(series) ==True or is_float_dtype(series):
if series.isnull().all()==True:
return np.NaN
else:
return series.fillna(0).mean()
op = pd.DataFrame(df.apply(check)).transpose()
Current output:
A B C st date
0 1 1 NaN Mango 2001-01-01 00:00:00
I am getting the wrong output, except for columns C and st.
Expected output:
A B C st date
0 5.5 2.833 NaN Mango 2001-01-06 00:00:00
Any suggestions on the mistake could be helpful?

