如何使用另一列作为参数来修改DataFrame列值

时间:2017-07-16 07:41:59

标签: python python-3.x pandas pandas-groupby

我有这样的数据框(数据):

                     mac  len      corp                               detail
18025           14:1F:BA    8  IeeeRegi          IEEE Registration Authority
18026  14:1F:BA:00:00:00   10  Shenzhen  Shenzhen Mining Technology Co.,Ltd.
18027  14:1F:BA:10:00:00   10   Gloquad                                  NaN
18028  14:1F:BA:20:00:00   10  Deutsche      Deutsche Energieversorgung GmbH
18029  14:1F:BA:30:00:00   10   Private                                  NaN

如何使用数据[' mac']。str.slice(0,data [' len'])]等方法获得以下结果。

              mac  len      corp                               detail
18025    14:1F:BA    8  IeeeRegi          IEEE Registration Authority
18026  14:1F:BA:0   10  Shenzhen  Shenzhen Mining Technology Co.,Ltd.
18027  14:1F:BA:1   10   Gloquad                                  NaN
18028  14:1F:BA:2   10  Deutsche      Deutsche Energieversorgung GmbH
18029  14:1F:BA:3   10   Private                                  NaN

我知道apply方法没问题:

def sub_mac(x):
    return x.mac[:x.len]
data.mac = data.apply(sub_mac, axis=1)

data.mac = data.apply(lamda x: x.mac[:x.len], axis=1)

但我想知道是否还有其他方法可以处理它? 例如,像sql:

这样的方法
select SUBSTRING(mac, 0, len) as mac_sub from data;

THX。

1 个答案:

答案 0 :(得分:0)

试试这个:

来源DF:

from keras.layers import TimeDistributed

input_layer = Input((num_of_images, image_dims...))
# m_cnn is your VGG like model, taking one image as input.
layer1 = TimeDistributed(m_cnn)(input_layer)
layer2 = YourRNNLayer(...)(layer1)

解决方案:

In [8]: df
Out[8]:
                     mac  len      corp                               detail
18025           14:1F:BA    8  IeeeRegi          IEEE Registration Authority
18026  14:1F:BA:00:00:00   10  Shenzhen  Shenzhen Mining Technology Co.,Ltd.
18027  14:1F:BA:10:00:00   10   Gloquad                                  NaN
18028  14:1F:BA:20:00:00   10  Deutsche      Deutsche Energieversorgung GmbH
18029  14:1F:BA:30:00:00   10   Private                                  NaN

结果:

In [9]: df['mac'] = df.groupby('len')['mac'].transform(lambda x: x.str[:x.name])