1

I have a heavily nested json file which I have flattened and getting output as below having rows in one column with numeric values attached to it. Is there is any way I can remove it and have them into row wise as shown in the output
Input file

102_ip_addr, 102_ip_family, 102_ip_mask_addr, 102_email,    102_failed_attempts,103_ip_addr, 103_ip_family, 103_ip_mask_addr, 103_email,    103_failed_attempts,
3705824725, 2,  4294967295, [email protected],    0,3705824825,   4,  4294967625, [email protected],    0

Output:

ip_addr, ip_family, ip_mask_addr, email, failed_attempts
3705824725, 2,  4294967295, [email protected],    0
3705824825, 4,  4294967625, [email protected],    0

1 Answer 1

2

If every new row have fixed width 5 , you can using reshape

pd.DataFrame(df.values.reshape(-1,5),columns=['addr','family','mask_addr','email','attempts'])
Out[580]: 
         addr family   mask_addr         email attempts
0  3705824725      2  4294967295   [email protected]        0
1  3705824825      4  4294967625   [email protected]        0

Update

df.columns=df.columns.str.split('_',1).str[1]

df.melt().assign(newrow=lambda x : x.groupby(x['variable']).cumcount() ).pivot('newrow','variable','value')
Out[596]: 
variable         email failed_attempts     ip_addr ip_family ip_mask_addr
newrow                                                                   
0          [email protected]               0  3705824725         2   4294967295
1          [email protected]               0  3705824825         4   4294967625
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the quick answer in my file there are many columns I just gave 5 columns as reference. I am trying removing the numeric values before the first _ and then bring them into rows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.