gpt4 book ai didi

python - `pandas.DataFrame.apply` 逐行操作

转载 作者:行者123 更新时间:2023-11-30 23:39:47 32 4
gpt4 key购买 nike

我想返回一个数据帧,其中每行均已排序(假设是降序)。所以如果我有 pandas.DataFrame命名data :

In [38]: data
Out[38]:
c1 c2 c3 c4 c5 c6
Date
2012-10-22 0.973371 0.226342 0.968282 0.872330 0.273880 0.746156
2012-10-19 0.497048 0.351332 0.310025 0.726669 0.344202 0.878755
2012-10-18 0.315764 0.178584 0.838223 0.749962 0.850462 0.400253
2012-10-17 0.162879 0.068409 0.704094 0.712860 0.537545 0.009789

我希望返回以下内容:

In [39]: sorted_frame
Out[39]:
0 1 2 3 4 5
Date
2012-10-22 0.973371 0.968282 0.872332 0.746156 0.273880 0.226342
2012-10-19 0.878755 0.726669 0.497048 0.351332 0.344202 0.310025
2012-10-18 0.850462 0.838223 0.749962 0.400253 0.315764 0.178584
2012-10-17 0.712860 0.704094 0.537545 0.162879 0.068409 0.009789

我已经尝试过DataFrame.sort(axis = 1)但是,这并没有达到预期的结果:

In [40]: data.sort(axis = 1)
Out[43]:
c1 c2 c3 c4 c5 c6
Date
2012-10-22 0.973371 0.226342 0.968282 0.872330 0.273880 0.746156
2012-10-19 0.497048 0.351332 0.310025 0.726669 0.344202 0.878755
2012-10-18 0.315764 0.178584 0.838223 0.749962 0.850462 0.400253
2012-10-17 0.162879 0.068409 0.704094 0.712860 0.537545 0.009789

我创建了以下函数来完成我正在寻找的功能(使用 pandas.TimeSeries.order() ):

import numpy

def sorted_by_row(frame, ascending = False):
vals = numpy.tile(numpy.nan,frame.shape)
for row in numpy.arange(frame.shape[0]):
vals[row, :] = frame.ix[row, :].order(ascending = ascending)
return pandas.DataFrame(vals, index = frame.index)

但是,我的目标是能够在 DataFrame.apply() 中使用逐行函数。方法(这样我就可以将所需的功能应用到我构建的其他功能中)。我试过:

 #TimeSeries.order() sorts a pandas.TimeSeries object
data.apply(lambda x: x.order(), axis = 1)

但同样,我没有得到想要的 DataFrame上面(我已经输出了足够的 DataFrame' ,所以我将节省页面的空间)。

非常感谢您的帮助,

-B

最佳答案

嗯,开箱即用的 pandas 并不是那么容易实现的。首先,熟悉argsort:

In [8]: df
Out[8]:
0 1 2 3 4
2012-10-17 1.542735 1.081290 2.602967 0.748706 0.682501
2012-10-18 0.058414 0.148083 0.094104 0.716789 2.482998
2012-10-19 2.396277 0.524733 2.169018 1.365622 0.590767
2012-10-20 0.513535 1.542485 0.186261 2.138740 1.173894
2012-10-21 0.495713 1.401872 0.919931 0.055136 1.358439
2012-10-22 1.010086 0.350249 1.116935 0.323305 0.506086

In [12]: inds = df.values.argsort(1)

In [13]: inds
Out[13]:
array([[4, 3, 1, 0, 2],
[0, 2, 1, 3, 4],
[1, 4, 3, 2, 0],
[2, 0, 4, 1, 3],
[3, 0, 2, 4, 1],
[3, 1, 4, 0, 2]])

这些是每行的间接排序索引。现在你想做一些类似的事情:

new_values = np.empty_like(df)
for i, row in enumerate(df.values):
# sort in descending order
new_values[i] = row[inds[i]][::-1]

sorted_df = DataFrame(new_values, index=df.index)

不太令人满意,但它完成了工作:

In [15]: sorted_df
Out[15]:
0 1 2 3 4
2012-10-17 2.602967 1.542735 1.081290 0.748706 0.682501
2012-10-18 2.482998 0.716789 0.148083 0.094104 0.058414
2012-10-19 2.396277 2.169018 1.365622 0.590767 0.524733
2012-10-20 2.138740 1.542485 1.173894 0.513535 0.186261
2012-10-21 1.401872 1.358439 0.919931 0.495713 0.055136
2012-10-22 1.116935 1.010086 0.506086 0.350249 0.323305

更一般地说,你可以这样做:

In [23]: df.apply(lambda x: np.sort(x.values)[::-1], axis=1)
Out[23]:
0 1 2 3 4
2012-10-17 2.602967 1.542735 1.081290 0.748706 0.682501
2012-10-18 2.482998 0.716789 0.148083 0.094104 0.058414
2012-10-19 2.396277 2.169018 1.365622 0.590767 0.524733
2012-10-20 2.138740 1.542485 1.173894 0.513535 0.186261
2012-10-21 1.401872 1.358439 0.919931 0.495713 0.055136
2012-10-22 1.116935 1.010086 0.506086 0.350249 0.323305

但您负责自己分配新列

关于python - `pandas.DataFrame.apply` 逐行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13261855/

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