I'm working with a dataframe that contains several observations of BMW cars. The thing is that in the model columns I got several models like
Model
320
420
425
335
325
118
Z4
.
.
.
I want to change the number to its series, the ones that start by 1 are serie1, if starts with 2 are serie2. I've already checked str.contains(pat = '1')
but I still don't know how to apply it to the whole column.
CodePudding user response:
Pandas has a pandas.Series.str.replace method which can be used for this purpose. You would do something like:
df['Model'].str.replace('^1', 'serie1')
CodePudding user response:
If I understand you correctly, you want to get first character from the model column:
df["Serie"] = "Serie " df["Model"].str[0]
print(df)
Prints:
Model Serie
0 320 Serie 3
1 420 Serie 4
2 425 Serie 4
3 335 Serie 3
4 325 Serie 3
5 118 Serie 1
6 Z4 Serie Z