I'm trying to return the right n characters based on how many characters are right of a specific character. In this case it is the "#" sign.
df = pd.DataFrame(kitties)
df.columns = ['Asset','Endtime','Price']
df['neg_one'] = -1
df['pos_one'] = 1
df['Asset_num_1']= df['Asset'].str[-5:]
df['Asset_num_2'] = df['Asset_num_1'].str.find(search_value)
df['Asset_num_3'] = df['Asset_num_2'] df['pos_one']
df['Asset_num_4']= df['Asset'].str[(df['Asset_num_3']*df['neg_one']):]
Everything looks like its working except the last line where I'm trying to set the number of characters to return by using a formula.
Gives me the below dataset:
34 Kitty #5090 2021-12-23 22:00:00 00:00 419 -1 1 #5090 0 1 NaN 35 Kitty #577 2021-12-23 10:00:00 00:00 427 -1 1 #577 1 2 NaN
CodePudding user response:
For filter charaters by another columns use:
df['Asset_num_4']= df.apply(lambda x: x['Asset'][x['Asset_num_3']*x['neg_one']:], axis=1)
Or list comprehension:
df['Asset_num_4']= [a[b*c:] for a,b,c in df[['Asset','Asset_num_3','neg_one']].to_numpy()]