工作流=>
示例:
Unit Price
----------
330
350
380
I want to convert this data
Fabric
------
Card
Combed
Viscos我的代码:
##Fabric Data
getFabric = df_new['Unit Price']
result = []
for fabric in getFabric:
if fabric == 310:
result.append("Card")
elif fabric == 330:
result.append("Combed Dawah")
elif fabric == 350:
result.append("Combed Regular")
elif fabric == 490:
result.append("Viscos")
elif fabric == 550:
result.append("Pleated")
else:
result.append(fabric)
df_new['Fabric'] = result错误:
发布于 2022-05-22 15:23:32
迭代列值的计算。试试这个,
熊猫内置函数称为.replace(),对于替换列中的值非常有用,而无需迭代遍历它。
df_new['Unit Price'].replace({310: 'Card', 330: 'Combed Dawah', 350: 'Combed Regular', 490: 'Viscos', 550: 'Pleated'}, inplace=True)上面的代码将成功地关联dataframe列值inplace。
发布于 2022-05-22 17:12:55
这很简单伙计..。
your_df["Fabric"] = your_df["Unit Price"].apply(lambda x: str(x).replace("330", "Card"))
# do this for every conversion
your_df.to_csv("filename.csv")以上代码可以保存为CSV文件,可以在MS中查看。
https://stackoverflow.com/questions/72338772
复制相似问题