简体   繁体   English

如何使用Pandas在数据框中搜索部分字符串并返回特定的单元格?

[英]How to search for a partial string in a dataframe and return a specific cell using Pandas?

I've got a dataframe with non-standard columns (not the same info all the way down). 我有一个带有非标准列的数据框(一直到下边都没有相同的信息)。 I want to search for a particular phrase that may or may not appear in some column of a row and then return the value of the succeeding cell. 我想搜索可能不在行的某些列中出现的特定短语,然后返回后续单元格的值。 For example: 例如:

A               B               C
How many?       5               Blah blah
Blah            How many?       7
How many?       13              Blah
Blah            Blah            Blah

I'm trying to figure out how to search for the phrase "How many?" 我试图弄清楚如何搜索短语“多少?” and then return the value in the next cell (5/7/13/null) 然后在下一个单元格(5/7/13 / null)中返回值

With Boolean and shift 带布尔和shift

df[df.eq('How many?').shift(1,axis=1).fillna(False)]
Out[142]: 
     A    B    C
0  NaN    5  NaN
1  NaN  NaN    7
2  NaN   13  NaN
3  NaN  NaN  NaN

Update 更新

s1=df.eq('How many?').shift(1,axis=1).fillna(False)
s2=df.eq('How many?')
df[s1|s2]
Out[154]: 
          A          B    C
0  How many?         5  NaN
1       NaN  How many?    7
2  How many?        13  NaN
3       NaN        NaN  NaN

Use numpy array instead for easier indexing: 请改用numpy数组,以便于索引编制:

mask = df.values == 'How many?'
your_list = [df.values[i, j+1] for i, j in zip(*np.where(mask)) if j < df.values.shape[1]-1]
# yourlist = ['5', '7', '13']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用通配符在 DataFrame 中搜索特定字符串 - How to search a DataFrame for a specific string using a wildcard Pandas dataframe 搜索字符串并返回 False 值 - Pandas dataframe search for string and return False values 移位熊猫数据框的参差不齐的行以使用部分字符串搜索来清理数据 - Shift ragged rows of Pandas DataFrame to clean data with partial string search 如何使用单元格中的字符串重新排序 pandas 中的 dataframe? - How to re-oder a dataframe in pandas using a string in cell? 查找熊猫数据框中首次出现的特定部分字符串的索引位置 - Find index location of first occurrence of a specific partial string in pandas dataframe 如何使用熊猫为包含特定字符串的单元格中的文本着色 - How to color text in a cell containing a specific string using pandas Python Pandas dataframe中字符串列表的部分匹配并返回所有匹配的部分字符串 - Python Pandas partial match of list of string in dataframe and return all match partial string Pandas Dataframe 上的部分字符串合并 - Partial String Merge on Pandas Dataframe Pandas Dataframe 中的部分字符串匹配 - Partial String Matching in Pandas Dataframe 有没有办法在pandas DataFrame中搜索列中的2个特定数字并返回索引(如果存在)? - Is there a way to 'search' in a pandas DataFrame for 2 specific numbers in columns and return the index if it exists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM