gpt4 book ai didi

python - Pandas 根据列 dtypes 进行应用

转载 作者:行者123 更新时间:2023-12-02 02:35:35 31 4
gpt4 key购买 nike

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

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com