我有一个具有以下列和值的数据框:
product 1_vendor 2_vendor 3_vendor price_shop1 price_shop2 price_shop3 url_shop1 url_shop2 url_shop3
blue shop1 shop3 shop2 500 600 550 1.com/blue 2.com/blue 3.com/blue
pink shop3 shop2 shop1 700 650 600 1.com/pink 2.com/pink 3.com/pink
cyan shop1 shop2 shop3 0 200 300 1.com/cyan 2.com/cyan 3.com/cyan
“1_供应商”是最便宜供应商的名称,“3_供应商”是最昂贵供应商的名称
从这些信息中,我想以以下列结束:产品、1个供应商、1个价格、1个url、2个供应商、2个价格、2个url等等,按1是最便宜的和3是最贵的顺序排列。像这样:
product 1_vendor 1_price 1_url 2_vendor 2_price 2_url
blue shop3 555 3.com/blue shop1 700 1.com/blue
我想我可以使用.replace for each column将“shop”字符串更改为price和url,但是下面的代码给出了一个错误
df['1_url'] = df['1_vendor'].replace('shop1', df['url_shop1'])
df['1_url'] = df['1_vendor'].replace('shop2', df['url_shop2'])
ValueError: Series.replace cannot use dict-value and non-None to_replace
如果我在它前面加上str(df['url\u shop1']),它会运行,但会用整个列的值填充单元格
如何以这种方式对数据帧进行排序?我将最终导出到CSV
我希望我正确理解了你的问题。
我有点醉了,所以可能会有错误,强制家庭办公的几周比预期的要难xD。
无论如何,有一个解决方案:
现在我们可以使用simple for函数循环原始df并提取结果。
编辑:对于导出,只需使用to_csv命令,如果有问题,请告诉我
好的,应该是这样。
如果我没有收到问题或您有任何问题,请告诉我。
祝你好运&书信电报;br/
(如果答案正确,请标记,谢谢)
相关问题 更多 >
编程相关推荐