python - 重新排列 linregress python 中的标题

标签 python python-3.x pandas scipy

我在 Python 中使用 Linregress 来获取斜率、截距等

输出是一个 csv 文件。但是,数据输出的列中没有标题。数据输出为:slope=5.562,intercept=223.5 etc

附上一张图片,其中包含我在 Jupyter 笔记本中看到的内容(左图)与我需要的数据(右图)。 t 正在重新排列数据,以便标题不是水平显示而是垂直显示。

enter image description here

如果我能得到这方面的帮助,那就太好了。

感谢您的阅读!!

最佳答案

你可以做一些类似的事情:

res = df.groupby('Test_event')[['x','y']].apply(linregress).apply(pd.Series)

res.columns = ['slope','intercept','rvalue','pvalue','stderror']

尽管链接的apply不太理想

示例:

>>> df
   Test_event  x   y
0           5  1   4
1           5  1   5
2           5  2   6
3           6  3   8
4           6  4  10
5           6  5  11

>>> res = df.groupby('Test_event')[['x','y']].apply(linregress).apply(pd.Series)
>>> res.columns = ['slope','intercept','rvalue','pvalue','stderror']
>>> res
            slope  intercept    rvalue    pvalue  stderror
Test_event                                                
5             1.5   3.000000  0.866025  0.333333  0.866025
6             1.5   3.666667  0.981981  0.121038  0.288675

或者你可以这样做,但我不确定它是否更有效:

res = (df.groupby('Test_event')
       .apply(lambda group: pd.Series(linregress(group['x'],group['y']))))

res.columns = ['slope','intercept','rvalue','pvalue','stderror']

关于python - 重新排列 linregress python 中的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52461883/

相关文章:

python - 如何使用 matplotlib 绘制盒须图

python - 将变量注入(inject)函数; Python

python - read_table pandas python 数字错误

python - pandas.algos._return_false 在 CentOS 上使用 dill.dump_session 导致 PicklingError

python - 是否可以在 Vista 上构建 exe 并使用 py2exe 在 XP 上部署

python - Pandas dataframe 按一些值填充

python - 如何取消在类将成员变量更改为属性之前保存的 Python 实例?

python - AWS Lambda 调用 url 花费的时间超过 Lambda 执行限制

python - 分割数据帧的行并将它们作为单独的行存储在同一数据帧中

python - 如何通过 pandas dataframe axis python 获取条形图