Python 熊猫:查找单元格索引:不区分大小写
我有一个函数,它使用'isin'内置函数在panda数据帧中查找值。问题是我想让这个函数不区分大小写。我可以将每一列解析为一个序列,并使用'str.contains',但我发现它有点难看。你知道一个好方法吗 以下是返回给定世界的索引和列的函数:Python 熊猫:查找单元格索引:不区分大小写,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个函数,它使用'isin'内置函数在panda数据帧中查找值。问题是我想让这个函数不区分大小写。我可以将每一列解析为一个序列,并使用'str.contains',但我发现它有点难看。你知道一个好方法吗 以下是返回给定世界的索引和列的函数: def find_pos(self, titres): bool_table = self.document.isin(titres) for i in range(bool_table.shape[0]): for
def find_pos(self, titres):
bool_table = self.document.isin(titres)
for i in range(bool_table.shape[0]):
for j in range(bool_table.shape[1]):
boolean = bool_table.iloc[i][j]
if boolean:
return i, j
print(titres, " not found in csv", file=sys.stderr)
return -1, -1
一个有效的解决方案是使用lambdas:
bool_table = self.document.apply(lambda x: x.astype(str).str.lower()).isin([x.lower() for x in titres])
由于我是python新手,也许这不是最好的方法?另一种方法是首先找到匹配的列,然后找到行索引。 使用正则表达式进行不区分大小写的匹配 一个样本方法是
def find_pos(search):
pattern = '(?i)' + search
# search in each column
for column in df:
df2 = df[df[column].str.contains(pattern, regex=True)]
if not df2.empty:
# find row index and column index
return (df2.index[0], df2.columns.get_loc(column))
idx = find_pos('to')
print(idx)
我尝试了以下示例数据
import pandas as pd
df = pd.DataFrame(columns = ['Name', 'Location'])
df.loc[len(df)] = ['Mathew', 'Houston']
df.loc[len(df)] = ['Tony', 'New York']
df.loc[len(df)] = ['Jerom', 'Los Angeles']
df.loc[len(df)] = ['Aby', 'Dallas']
df.loc[len(df)] = ['Elma', 'Memphis']
df.loc[len(df)] = ['Zack', 'Chicago']
df.loc[len(df)] = ['Lisa', 'New Orleans']
df.loc[len(df)] = ['Nita', 'Las Vegas']
您能否添加具有所需输出的示例数据?请看一看如何提供
您的_columns.str.lower()
?一个简单的例子是titres:[“TOTO”],数据框在单元格3中包含“TOTO”,3:输出为3,3@RockyLi是的,这是我的第一个猜测,我只是想知道是否有更好的方法来做到这一点:)我对熊猫很陌生!你也可以用它来匹配。只是blah.isin(stuff.str.lower())