熊猫:如何转移所有数据帧

时间:2016-12-02 09:55:08

标签: python pandas numpy

我有df

'date', 'ip', 'type_connection', 'user_agent', 'smth', 'type', 'weight', 'smth1', 'url'
1480394201  127.0.75.123:3000   CONNECT -   200 -   0   uic.login.skype.com:443 -   
1480394202  127.0.75.123:3000   CONNECT -   200 -   0   uic.login.skype.com:443 -   
1480394203  127.0.75.123:3000   CONNECT -   200 -   0   uic.login.skype.com:443 -   -
1480395766  127.0.0.1   127.255.255.254:3000    CONNECT curl/7.38.0 200 -   0   google.com:443  -
1480395766  127.0.0.1   127.255.255.254:3000    HEAD    curl/7.38.0 403 text/html   220 https://google.com/ -

我需要从列type_connection中删除字符串,并将表的这一部分移到左侧

欲望输出

'date', 'ip', 'type_connection', 'user_agent', 'smth', 'type', 'weight', 'smth1', 'url'
1480394201  127.0.75.123:3000   CONNECT -   200 -   0   uic.login.skype.com:443 -   
1480394202  127.0.75.123:3000   CONNECT -   200 -   0   uic.login.skype.com:443 -   
1480394203  127.0.75.123:3000   CONNECT -   200 -   0   uic.login.skype.com:443 -   -
1480395766  127.0.0.1   CONNECT curl/7.38.0 200 -   0   google.com:443  -
1480395766  127.0.0.1   HEAD    curl/7.38.0 403 text/html   220 https://google.com/ -

我用

df.type_connection = np.where(df.type_connection.str.contains(':'), df.user_agent, df.type_connection)

但它只移动了一列user_agent

1 个答案:

答案 0 :(得分:1)

您可以在已过滤的DataFrame中尝试shift,然后使用concat

df = df.set_index('date')
mask = df.type_connection.str.contains(':')
df1 = df[mask].shift(-1, axis=1)
#print (df1)

print (pd.concat([df[~mask], df1]).reset_index())