繁体   English   中英

将用户定义的 function 应用到 dataframe

[英]applying a user defined function to a dataframe

我尝试编写的 function 将采用提供的 dataframe 并计算 F 统计值并将其提供为 output。

Final数据格式

Key      Color   Strength   Fabric  Sales
a         0         1         1       10
b         1         2         2       15

在这里,颜色、强度和面料是独立的,而销售额是相关的。

这个想法是创建一个循环,为每个唯一键值创建一个新的 dataframe:并在这个 dataframe 上执行 function,然后创建一个新的 dataframe,它是从唯一键值获得的所有新数据帧的连接

def regression():
    X=Final1.copy()
    y=Final1[['Sales']].copy()
    X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=.2, random_state=0)
    sel=f_classif(X_train, y_train)
    p_values=pd.Series(sel[0], index=X_train.columns)
    p_values=p_values.reset_index()
    pd.options.display.float_format = "{:,.2f}".format
    return p_values

Finals=[]
Finals=pd.DataFrame(Finals)
for group in Final.groupby('Key'): 
    # group is a tuple where the first value is the Key and the second is the dataframe
    Final1=group[1]
    Final1=pd.DataFrame(Final1)
    result=regression()
    Finals=pd.concat([Finals, result], axis=1)


# do xyz with result

print(Finals)

这是我想出的代码,但它抛出错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-131-c3a3b53971d5> in <module>
      5     Final1=group[1]
      6     Final1=pd.DataFrame(Final1)
----> 7     result=regression()
      8     Finals=pd.concat([Finals, result], axis=1)
      9 

<ipython-input-120-d5c718baaba8> in regression()
      2     X=Final1.iloc[:,7:-1].copy()
      3     y=Final1[['Sale Rate']].copy()
----> 4     X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=.2, random_state=0)
      5     sel=f_classif(X_train, y_train)
      6     p_values=pd.Series(sel[0], index=X_train.columns)

~\anaconda3\lib\site-packages\sklearn\model_selection\_split.py in train_test_split(*arrays, **options)
   2120     n_samples = _num_samples(arrays[0])
   2121     n_train, n_test = _validate_shuffle_split(n_samples, test_size, train_size,
-> 2122                                               default_test_size=0.25)
   2123 
   2124     if shuffle is False:

~\anaconda3\lib\site-packages\sklearn\model_selection\_split.py in _validate_shuffle_split(n_samples, test_size, train_size, default_test_size)
   1803             'resulting train set will be empty. Adjust any of the '
   1804             'aforementioned parameters.'.format(n_samples, test_size,
-> 1805                                                 train_size)
   1806         )
   1807 

ValueError: With n_samples=1, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.


这段代码可能出了什么问题?

一个简单的修复方法是:

for group in Final.groupby('Key'): 
    # group is a tuple where the first value is the Key and the second is the dataframe
    result = regression(group[1])
    # do xyz with result

编辑:

您不必再次将组转换为数据框,并且可以直接使用它,因为它已经是正确的格式。

# this line is not necessary
Final1 = pd.DataFrame(Final1)

从报错来看,很明显你传入train_test_split function 的group没有足够的记录。 这在错误消息中非常明显。 您将不得不使用 try, except 来处理此类错误。

一旦我过滤掉所有少于 10 个观察的键,代码就可以工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM