I have the following dataframe:
import pandas as pd
df = pd.DataFrame({'Id_Sensor': [1, 2, 3, 4],'Old_Column': ['P55X', 'MEC8901', 'P58Y', 'M59X']})
print(df)
Id_Sensor Old_Column
1 P55X
2 MEC8901
3 P58Y
4 M59X
I need to create a new column on this dataframe. If the first letter is P, the column should receive 'computer_typeA'. If the first three letters are MEC the column should receive 'computer_typeB'
I tried to do the following:
#This code segment is incorrect
for i in range(0, len(df)):
if(df['Old_Column'].iloc[i][:1] == 'P'):
df['New_Column'].iloc[i] == 'computer_typeA'
elif(df['Old_Column'].iloc[i][:3] == 'MEC'):
df['New_Column'].iloc[i] == 'computer_typeB'
else:
df['New_Column'].iloc[i] == 'computer_other'
The answer is incorrect:
print(df)
Id_Sensor Old_Column New_Column
1 P55X Nan
2 MEC8901 Nan
3 P58Y Nan
4 M59X Nan
I would like the answer to be like this:
Id_Sensor Old_Column New_Column
1 P55X computer_typeA
2 MEC8901 computer_typeB
3 P58Y computer_typeA
4 M59X computer_other