我想解析和处理我的 Dataframe 数据。
我尝试使用join、assign ...等。我使用以下代码成功解析了“允许”列:
allowed_expanded = df1.allowed.apply(lambda x:pd.Series(x))
allowed_expanded.columns = ['{}.{}'.format('allowed',i) for i in allowed_expanded]
结果是:
# 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
但这不是我想要的。
我该怎么办?
现在我的数据看起来:
# 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'}]
...
还有我想要的
# 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 -
...
2条答案
按热度按时间jjjwad0x1#
希望能有所帮助!
krcsximq2#
你能试试这个吗:
输出: