[英]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.