在Pandas dataframe中使用apply返回多列,可以通过两种方法实现:使用apply函数和使用assign函数。 方法一:使用apply函数 首先,定义一个函数,该函数将应用于每一行或每一列。 使用apply函数,将该函数应用于DataFrame的每一行(axis=1)或每一列(axis=0)。 在apply函数中,设置参数result_type='expand',以展开返回的Series...
result_type参数可以取'reduce','expand','broadcast'以及None,默认是None。result_type参数是在每次处理返回的对象是可迭代时才生效,如果只有一个元素则返回Series,result_type参数便不生效。对于groupby数据使用apply时不生效,此时会把result_type认为是apply要应用的函数的参数。
result_type : {'expand', 'reduce', 'broadcast', None}, default None 只有在axis=1列时才会发挥作用。 expand : 列表式的结果将被转化为列。 reduce : 如果可能的话,返回一个Series,而不是展开类似列表的结果。这与 expand 相反。 broadcast : 结果将被广播到 DataFrame 的原始形状,原始索引和列将被保留...
• “result_type”参数:可输入'expand'、'reduce'、'broadcast'或None,用以设置最终的返回类型。 1) 默认为None,根据函数的返回类型推断最终的返回类型。 2) 只有“axis”参数设置为1 或'columns',才可输入'expand','reduce'以及'broadcast': 'expand':将类似列表的结果数据扩展为DataFrame的列; 'reduce'...
result_type :{'expand', 'reduce', 'broadcast', None}, default None 只有在axis=1列时才会发挥作用。 expand : 列表式的结果将被转化为列。 reduce : 如果可能的话,返回一个Series,而不是展开类似列表的结果。这与 expand 相反。 broadcast : 结果将被广播到 DataFrame 的原始形状,原始索引和列将被保留...
result_type: 当axis=1时,设置返回结果的类型和样式,支持{'expand', 'reduce', 'broadcast', None}四种类型,默认为None。 expand: 列表式的结果将被转化为列。 reduce: 如果可能的话,返回一个Series,而不是返回列表式的结果。这与expand相反。 broadcast: 结果将被广播成DataFrame的原始形状,DataFrame的原始索引...
>>>df.apply(lambda x:[1,2],axis=1,result_type='expand')01012112212 在函数中返回一个Series类似于传递result_type='expand'。结果的列名将是Series的索引。 代码语言:javascript 复制 >>>df.apply(lambda x:pd.Series([1,2],index=['foo','bar']),axis=1)foo bar012112212 ...
df_test['made_profit'] = df_test[df_test['product']==0].apply(product0_makes_profit, args=[4000], axis=1, result_type="expand") df_test['made_profit'] = df_test[df_test['product']==1].apply(product1_makes_profit, args=[9000], axis=1, result_type="expand")...
.. deprecated::0.23.0This argument will be removedina future version, replacedby``result_type='reduce'``. 实例 对于第一种,如果把他当作一列就肯定会报错了。 更新 在更加新的版本中直接使用result='reduce'即可 result_type : {'expand','reduce','broadcast', None},defaultNone ...
result_type:{‘expand’, ‘reduce’, ‘broadcast’, None}, 默认为None。主要是确定返回值的类型(list-like, series, dataframe),除了该设定还需要考虑函数的功能。简单解释见下方代码。 简单应用 df.apply(np.sum,axis=1)# 'expand': 返回值为列表类型df.apply(lambdax:[1,2],axis=1,result_type='ex...