df[column]=应用(lambda行:row.sort_值()[1])行为怪异

2024-09-30 22:14:38 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是: 为什么top_2_的值应该不同于top_2_is,或者换句话说,如果将apply函数分配给某个列,为什么结果是错误的?在

编辑:因为我认为这个问题有点误解,所以我又举了一个例子。 EDIT2:我使用python2.7.12::Anaconda 4.0.0(64位)::Pandas 0.18.0

import pandas as pd

d = {'one' : [1., 2., 3., 4.],
     'two' : [4., 3., 2., 1.]}
df52 = pd.DataFrame(d)

top_1_should = df52.apply(lambda row: row.sort_values()[0], 1)
top_2_should = df52.apply(lambda row: row.sort_values()[1], 1)
df52['top_1_is'] = df52.apply(lambda row: row.sort_values()[0], 1)
df52['top_1_should'] = top_1_should
df52['top_2_is'] = df52.apply(lambda row: row.sort_values()[1], 1)
df52['top_2_should'] = top_2_should
print df52

   one  two  top_1_is  top_1_should  top_2_is  top_2_should
0  1.0  4.0       1.0           1.0       1.0           4.0
1  2.0  3.0       2.0           2.0       2.0           3.0
2  3.0  2.0       2.0           2.0       2.0           3.0
3  4.0  1.0       1.0           1.0       1.0           4.0

最好的, 一月


Tags: lambda函数编辑istop错误sortone
2条回答

我认为您可以将^{}与{a2}一起用于断开对齐行:

print (df52.apply(lambda row: row.sort_values().values, axis=1))
   one  two
0  1.0  4.0
1  2.0  3.0
2  2.0  3.0
3  1.0  4.0

或者:

^{pr2}$

如果使用print,则得到排序输出-如果在前面添加新列,则需要更改Series中所选行的位置DataFrame中的列是什么:

top_1_should = df52.apply(lambda row: row.sort_values()[0], 1)
top_2_should = df52.apply(lambda row: row.sort_values()[1], 1)
df52['top_1_is'] = df52.apply(lambda row: row.sort_values()[0], 1)
df52['top_1_should'] = top_1_should
df52['top_2_is'] = df52.apply(lambda row: row.sort_values()[1], 1)
df52['top_2_is'] = df52.apply(lambda row: print(row.sort_values()), 1)
one             1.0
top_1_is        1.0
top_1_should    1.0
top_2_is        1.0
two             4.0
Name: 0, dtype: float64
one             2.0
top_1_is        2.0
top_1_should    2.0
top_2_is        2.0
two             3.0
Name: 1, dtype: float64
two             2.0
top_1_is        2.0
top_1_should    2.0
top_2_is        2.0
one             3.0
Name: 2, dtype: float64
two             1.0
top_1_is        1.0
top_1_should    1.0
top_2_is        1.0
one             4.0
Name: 3, dtype: float64
import pandas as pd

d = {'one' : [1., 2., 3., 4.],
     'two' : [2., 3., 4., 5.]}
df52 = pd.DataFrame(d)

top_1_should = df52.apply(lambda row: row.sort_values()[0], 1)
top_2_should = df52.apply(lambda row: row.sort_values()[1], 1)
df52['top_1_is'] = df52.apply(lambda row: row.sort_values()[0], 1)
df52['top_1_should'] = top_1_should
df52['top_2_is'] = df52.apply(lambda row: row.sort_values()[3], 1)
df52['top_2_should'] = top_2_should
print(df52)

退货:

^{pr2}$

相关问题 更多 >