Workflow =>
- Read CSV file and get Unit Price column data
- Convert column data price and create a new column as name ‘Fabric’
- save the output as xlsx
Sample:
JavaScript
x
14
14
1
Unit Price
2
----------
3
330
4
350
5
380
6
7
I want to convert this data
8
9
Fabric
10
------
11
Card
12
Combed
13
Viscos
14
My code:
JavaScript
1
19
19
1
##Fabric Data
2
getFabric = df_new['Unit Price']
3
result = []
4
for fabric in getFabric:
5
if fabric == 310:
6
result.append("Card")
7
elif fabric == 330:
8
result.append("Combed Dawah")
9
elif fabric == 350:
10
result.append("Combed Regular")
11
elif fabric == 490:
12
result.append("Viscos")
13
elif fabric == 550:
14
result.append("Pleated")
15
else:
16
result.append(fabric)
17
df_new['Fabric'] = result
18
19
Error :
Advertisement
Answer
Insted of iterating column value. Try this,
pandas built-in function called .replace()
is useful for replacing the value in the column without iteration throung it
JavaScript
1
2
1
df_new['Unit Price'].replace({310: 'Card', 330: 'Combed Dawah', 350: 'Combed Regular', 490: 'Viscos', 550: 'Pleated'}, inplace=True)
2
Above code will successfully relace the dataframe column values inplace.