I want to parse and process my dataframe data.
I tried using join, assign … etc.
I succeed parsing ‘allowed’ column with below code:
allowed_expanded = df1.allowed.apply(lambda x:pd.Series(x))
allowed_expanded.columns = ['{}.{}'.format('allowed',i) for i in allowed_expanded]
and the result:
# allowed_expanded
allowed.0 allowed.1 allowed.2
0 {'IPProtocol': 'tcp', 'ports': ['53']} NaN NaN
1 {'IPProtocol': 'tcp', 'ports': ['22', '3389']} NaN NaN
2 {'IPProtocol': 'icmp'} {'IPProtocol': 'sctp'} NaN
3 {'IPProtocol': 'all'} NaN NaN
but this is not what I want.
what should do I do ?
now my data looks:
# print(df)
network allowed
0 vpc-1 [{'IPProtocol': 'tcp', 'ports': ['53']}]
1 vpc-1 [{'IPProtocol': 'tcp', 'ports': ['22', '3389']}]
2 vpc-1 [{'IPProtocol': 'icmp'}, {'IPProtocol': 'sctp'}]
3 vpc-1 [{'IPProtocol': 'all'}]
...
and…
what I want:
# print(df)
network allowed.IPProtocol allowed.ports
0 vpc-1 tcp 53
1 vpc-1 tcp 22, 3389
2 vpc-1 icmp, sctp -
3 vpc-1 all -
...
3
Answers
I hope it helps!
Can you try this:
Output:
Example
df
Step1
eval and explode and so on..
df1
Step2
groupby
df1 by indexdf2
join
df and df2out