Home > database >  Pandas mean() how to force export full
Pandas mean() how to force export full

Time:10-31

I want to ask how to force several function in pandas to export fully results for each columns although some columns are object. For example:

A = pd.DataFrame({"A": range(4), "B":["one", "two", "three", "four"], "C": range(4)})
A.mean()

Here, A.mean() will give result of column A and C only and skip column B.

What I want to give the result for all columns (column B can make it as NaN, for example). This is big problem because some function it gives results for full columns, while some are not so it creates the inconsistency when indexing for further calculation.

Thank you

CodePudding user response:

Since numeric aggregators skip non-numeric columns, you can coerce the columns to_numeric before aggregating:

A.apply(pd.to_numeric, errors='coerce').mean()

# A    1.5
# B    NaN
# C    1.5
# dtype: float64

CodePudding user response:

A.mean().reindex(columns=A.columns)

Should do the trick

CodePudding user response:

from pandas.api.types import is_numeric_dtype

def new_mean(column):
    if is_numeric_dtype(column):
        return column.mean()
    else:
        return np.NaN

A.apply(new_mean)

CodePudding user response:

mean = A.mean()
for i in [i for i in A if i not in mean]:
    mean[i] = float("NaN")
  • Related