熊猫的申请方法

时间:2015-10-06 21:14:30

标签: python pandas

python pandas apply()方法是否有效?例如,以下代码:

t = [[10,15], [30,40]]
tt = pd.DataFrame(t, columns=['a', 'b'])
print tt

def func(x):  
#     print '\n\nRunning function on Row', x.name

    if x.name == 1:
        for idx, val in x.iteritems():
#             print idx, val
            x[idx] *= 2

        return x

tt.apply(func, axis=1)
print '\n\n',tt

生成此输出:

    a   b
0  10  15
1  30  40


    a   b
0  10  15
1  60  80

我不明白为什么。如果我设置tt = tt.apply(func, axis=1),则会得到错误的结果。这让我觉得apply()方法适用,但在其他情况下,它没有,我实际上需要做new_df = df.apply(func, axis=1)apply()方法在哪些方案中就地工作,以及在哪种方案中返回新的SeriesDataFrame

0 个答案:

没有答案
相关问题