I am trying to find the postions of the cells which have "Pi".
My dataframe is as follows. As you can see, the cells with "Pi" are in 0,2, 1,1, 1,3 in format column,row.
Pi Column_B
0 F Kitten
1 L Pippy
2 Pi Lamb
3 K Pikjh
4 K Momo
Therefore, the output that I want is [(0,2),(1,1),(1,3)]. My code is as follows.
import pandas as pd
# My dataframe
data = {'Column_A':['F','L','K','K','K'],
'Column_B':['Kitten','Pippy','Lamb','Pikjh','Momo']}
df = pd.DataFrame(data)
# Search for the cell which includes "Pi"
search_for_Pi = df.apply(lambda row: row.astype(str).str.contains('Pi').any(), axis=1)
Found_Pi_Position = pd.DataFrame(search_for_Pi)
# Print the position of the cells which include "Pi"
Print_Pi_Position = list(Found_Pi_Position[Found_Pi_Position == True].index)
print(Print_Pi_Position)
However, I got the output [0, 1, 2, 3, 4]. Please help me finding the error.
CodePudding user response:
From what I could understand you want to filter out and located the strings that contain the PI, key string and check out what indexes have it. And put it into a list?
For that a simple loc And a index class selections will help you out
pi_rows = df.loc[df.Pi.str.contains('Pi')]
pi_rows_index = df.loc[df.Column_b.str.contains('Pi') | df.Pi.str.contains('Pi')].index
If you want to stack the returned object in a list, without having the index type
[str(i) for i in pi_rows_index]
Will return the wanted list
CodePudding user response:
list1=[]
def function1(s:pd.Series):
for k,v in s.to_dict().items():
if 'Pi' in v:
list1.append((s.name,k))
df1.T.reset_index(drop=True).T.apply(function1)
list1
