Issue
so I would like to change the first number in the number column to +233 given the first number is 0, basically I would like all rows in number to be like that of row Paul Both columns are string objects.
Expectations:
The first character of the values in the column named df["numbers"]
should be replaced to "+233"
if only == "0"
df = pd.DataFrame([[ken, 080222333222],
[ben, +233948433],
[Paul, 0800000073]],
columns=['name', 'number'])`
Solution
Hope I understood your edition, try this: Notice - I removed the first 0 and replaced it with +233
import pandas as pd
df = pd.DataFrame([["ken", "080222333222"], ["ben", "+233948433"], ["Paul", "0800000073"]], columns=['name', 'number'])
def convert_number(row):
if row[0] == '0':
row = "+233" + row[1:]
return row
df['number'] = df['number'].apply(convert_number)
print(df)
Answered By - Tomer S
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.