Issue
I am trying to change the strings in a Pandas Series by a condition. If the string name is say 'A', it should be 'AA'. The code snippet below works but it is very un elegant and inefficient. I am passing a Pandas series as an argument as I said. Is there any other way to accomplish this?
def conditions(x):
if x == 'A':
return "AA"
elif x == 'B':
return "BB"
elif x == 'C':
return "CC"
elif x == 'D':
return "DD"
elif x == 'E':
return "EE"
elif x == 'F':
return "FF"
elif x == 'G':
return "GG"
elif x == 'H':
return "HH"
elif x == 'I':
return "I"
elif x == 'J':
return "JJ"
elif x == 'K':
return "KK"
elif x == 'L':
return 'LL'
func = np.vectorize(conditions)
test = func(rfqs["client"])
Solution
If you are just trying to repeat a given string, you can add the string to itself across all rows at once. If you have some other condition, you can specify that condition and add the string to itself only for rows that meet the criteria. See this toy example:
df = pd.DataFrame({'client': ['A', 'B', 'Z']})
df.loc[df['client'].str.contains('[A-L]'), 'client'] = df['client'] * 2
to get
client
0 AA
1 BB
2 Z
Answered By - Hammurabi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.