【发布时间】:2023-02-15 22:06:05
【问题描述】:
感谢您抽出宝贵时间对此进行调查。我是一名初级程序员,对此很感兴趣。
#the dataframe is as follows for reference
data = [['\r\r8', 'tom', 10, '55\r62\r75'], ['18\r\r9', 'nick', 15, '77\r25\r85'], ['17\r19\r18', 'juli', 14, '55\r75\r85']]
df = pd.DataFrame(data, columns=['Roll No per Class', 'Name', 'Age', 'Highest Scores'])
这是一个示例数据框,原始数据框跨越超过 15,000 行和 10 列。
我希望将 /r 单元格放置到新行中,其他列重复。enter image description here
我试过this thread by jpp.enter image description here中提到的代码
我收到错误:
ValueError: All arrays must be of the same length
我猜这是因为 Roll number 列的长度与 Highest Scores 列的长度不匹配。
有人可以帮忙调查一下吗?这是我的第一篇文章,所以如果有任何遗漏和需要添加,请告诉我。
【问题讨论】:
-
df.set_index(['Name', 'Age']).apply(lambda x: x.str.split('\r').explode()).reset_index() -
请不要发布代码图片,而是发布实际代码。
标签: python pandas dataframe numpy