问题是: 为什么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
最好的, 一月
我认为您可以将^{} 与{a2}一起用于断开对齐行:
或者:
^{pr2}$如果使用
print
,则得到排序输出-如果在前面添加新列,则需要更改Series
中所选行的位置DataFrame
中的列是什么:退货:
^{pr2}$相关问题 更多 >
编程相关推荐