Jordan K
Jordan K

Reputation: 83

Return value from another column based on index of searched value

I have a Pandas Dataframe that looks like this.

0| 1 | 2 | 3
0|8/2/22| Jim's number -223-| Evan's number -4566-
1|8/3/22| Al's number -354L-| Tess's number -45366-
2|8/4/22| Eric's number -35034-| Daniel's number -LP4566-
3|8/5/22| John's number -35478-| Jacob's number -XF4T67-

I am trying to write a script that will allow me to search through columns 2 and 3 based off of that number, -224- (as an example), and then get the index so I can search column 1. Essentially, I am trying to match the value in columns 2 or 3 to column 1. Finding that number and pairing it to a date.

What is the best way to do this as I have been stuck trying to get the index off of only a partial string value (-224).

Upvotes: 0

Views: 890

Answers (1)

Z Li
Z Li

Reputation: 4318

df = pd.DataFrame({1:['8/2/22','8/3/22'], 2:['Jim\'s number -223-', 'Al\'s number -354L-']})
df.loc[df[2].str.contains('-223-'), 1]
    1       2
0   8/2/22  Jim's number -223-
1   8/3/22  Al's number -354L-
0    8/2/22
Name: 1, dtype: object

Upvotes: 1

Related Questions