0

In a pandas dataframe, I'd like to extract a character from a string column at the index given by another column. For example, given the following DataFrame

import pandas as pd

df = pd.DataFrame({"s": ["ACGT", "AAGA"], "i": [0, 2]})
 
#       s  i
# 0  ACGT  0
# 1  AAGA  2

I'd like to extract s[i] for each row, to get

      s  i  extracted
0  ACGT  0  A
1  AAGA  2  G

I would think I could do something like

df["s"].str.get(df["i"])

however .get() only takes a single integer parameter, and not a series.

What's the best way to get this done?

Uri Laserson
  • 2,221
  • 5
  • 28
  • 38

1 Answers1

0

Try lambda

df['extracted'] = df.apply(lambda x: x['s'][x['i']],axis=1)

      s  i extracted
0  ACGT  0         A
1  AAGA  2         G
wwnde
  • 22,093
  • 5
  • 13
  • 27