1

I have 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/ -

I need to delete strings from column type_connection and shift this part of table to the left

Desire output

'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/ -

I use

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

but it shift only one column user_agent

Petr Petrov
  • 4,090
  • 10
  • 31
  • 68
  • 1
    I think it would be a ton clearer if you used a [mcve](http://stackoverflow.com/help/mcve) using only a couple of columns and items like 'A','B', 'C' or something. This is polluted by complicated items and an unnecessary high number of columns. – Julien Marrec Dec 02 '16 at 10:02
  • @JulienMarrec what do you mean? – Petr Petrov Dec 02 '16 at 10:07
  • I mean I'm having trouble seeing the difference between your `df` and the desired output, and I cannot load your data with `pd.read_clipboard()`. Read [How to make good reproducible pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) maybe? – Julien Marrec Dec 02 '16 at 10:10

1 Answers1

1

You can try shift in filtered DataFrame and then use 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())
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252