如何将df1转换为df2
df1 = pd.DataFrame(
{
'item1_aspect1' : ["a", "b", "c"],
'item1_aspect2' : [1,2,3],
'item1_aspect3' : ["[12,34]", "[56,78]", "[99,10]"],
'item2_aspect1' : ["a", "b", "c"],
'item2_aspect2' : [1,2,3],
'item2_aspect3' : ["[12,34]", "[56,78]", "[99,10]"],
'item3_aspect1' : ["a", "b", "c"],
'item3_aspect2' : [1,2,3],
'item3_aspect3' : ["[12,34]", "[56,78]", "[99,10]"]
})
df2 = pd.DataFrame({
'aspect_1' : ["a", "b", "c", "a", "b", "c", "a", "b", "c"],
'aspect_2' : [1,2,3,1,2,3,1,2,3],
'aspect_3' : ["[12,34]", "[56,78]", "[99,10]", "[12,34]", "[56,78]", "[99,10]", "[12,34]", "[56,78]", "[99,10]"]
})
也就是说,列名是一个拆分成行的标识符。我不知道怎么做
我们需要首先调整列类型,然后调整
wide_to_long
下面是一个相当简单的方法:
输出为:
如果您想坚持使用pandas操作,就不要一直更改数据类型,而更喜欢列表理解。。 试试这个方法-
相关问题 更多 >
编程相关推荐