2021年1月6日星期三

How to get a substring based on position in pandas?

I have a pandas dataframe as shown below:

df =pd.DataFrame({'String':['JAIJAOD','ERJTD','AJDIDO','AJDIDO'],'Position':[5,2,nan,4]})  

I am trying to get a third column that shows what is the letter of first column that represents the number in column Position. The dataframe should be something like

df = pd.DataFrame({'String':['JAIJAOD','ERJTD','AJDIDO','AJDIDO'],'Position':[5,2,nan,4],'Letter':['O','J',nan,'D']})  

I have tried the following code, however, the output is not exactly what I want since the final table has some mistakes regarding the third column.

third = []  for i, n in zip(df['String'],df['Position']):        if n >0: #I thought it because the column Position have just floats           third.append(i[int(n)]        else:           third.append(np.nan)  df['Third'] = pd.Series(third)  
https://stackoverflow.com/questions/65606062/how-to-get-a-substring-based-on-position-in-pandas January 07, 2021 at 10:59AM

没有评论:

发表评论