python - Pandas 根据列 dtypes 进行应用

标签 python pandas numpy

我有一个示例数据框,我试图根据 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

示例数据框:

    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

我正在尝试根据 dtypes 列转换 DF 并生成单个

伪代码:

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)

代码:

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()

当前输出:

    A   B    C   st         date
0   1   1   NaN Mango   2001-01-01 00:00:00

除了列 Cst 之外,我得到了错误的输出。

预期输出:

    A     B      C   st       date
0   5.5 2.833   NaN Mango   2001-01-06 00:00:00

关于错误的任何建议可能会有帮助吗?

最佳答案

根据这个Why does apply change dtype in pandas dataframe columns
您需要在 apply 中使用 result_type='expand'

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, result_type='expand')).transpose()
op

enter image description here

enter image description here

关于python - Pandas 根据列 dtypes 进行应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64368415/

相关文章:

python - 从 IPython 复制代码,不带前导三点

python - 在 Pandas Dataframe 中检查一列并返回另一列

python - 单元测试中奇怪的对象实例行为

python - 将颜色图作为参数传递给函数

python - 计算 Pandas 数据框中的年数

Python - numpy 二维数组太长并且打印很奇怪?

python - 如何将魔杖图像对象转换为 numpy 数组(没有 OpenCV)?

python - NumPy 与 MATLAB

python - 路径名格式 - Windows - Python

python - Pandas 中的数据框转换