How to find the position of a word in a Dataframe

Question:

I have the problem that I cannot get the position of the row, from a text of a dataframe column.

I have a .dat data file where it contains a data table, I opened it with pandas so it reads as a dataframe (header = None).
Example:

filesday = pd.read_table("date_days.dat", header = None)

Result:

       0       1               2           3
0| 23   | re    | today-1.txt   | tomato    |
1| 45   | gfd   | today-2.txt   | apple     |
2| 12   | fgf   | today-3.txt   | coffee    |
3| 34   | vvd   | today-4.txt   | orange    |
4| 65   | fdsv  | today-5.txt   | pineappe  |

I extracted column 2 into a value called "info_day":

info_day = filesday.iloc[:,5]

What I get as a result:

0| today-1.txt   |
1| today-2.txt   |
2| today-3.txt   |
3| today-4.txt   |
4| today-5.txt   |
Name: 2, dtype: object

I have tried to find the position where a specific text is located, for example: "today-4.txt" using the index command, but I get the following error:

matched = info_day.index('today-4.txt')

TypeError: ‘RangeIndex’ object is not callable

I’d like it to cast me the value of 3 from this example, which is the row where the text is.
I know it seems simple, but the real file has more complex text and is larger, so I am interested in knowing the position of the text to extract the values of the other columns. I would thank you a lot. ( ◡́.◡̀)(^◡^ )

Asked By: iTanish

||

Answers:

# contains returns true/false when searching for a string like 'today-4'
# use the boolean to return the row

info_day[info_day.str.contains('today-4')].index[0]
3
# if multiple index are returned, you can get the list of all the indexes
info_day[info_day.str.contains('today')].index.tolist()
info_day[info_day.str.contains('today')].index.tolist()


[0, 1, 2, 3, 4]
Answered By: Naveed
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.