我有一个像这样的数据框
key values
Interface InterfaceA
State Up
Line Status Up
ID 9000
Interface InterfaceB
State Down
Line Status Down
ID 9001
我想把它变成这样
Interface State Line Status ID
InterfaceA Up Up 9000
InterfaceB Down Down 9001
我尝试使用loc逐列插入该列,但当它到达第2列时
ValueError: cannot reindex from a duplicate axis
出现上述错误
final_df['Interface'] = commands_df.loc[commands_df['key'].str.contains('Interface'), 'values']
final_df['State'] = commands_df.loc[commands_df['key'].str.contains('State'), 'values'] <-- Error starts here
ValueError: cannot reindex from a duplicate axis
带有
cumcount
和unstack
的简单set_index
另一种方法是使用
pd.crosstab
这里有一个可能的解决方案-
本answer的学分
印刷品:
相关问题 更多 >
编程相关推荐