Reputation: 181
Here is my df.
I want to get the first value in each column which contains (F)
>>> d = {0: ['1', '2(F)', '6', '8', '5'],
1: ['8(F)', '6', '8', '4(F)', '4'],
2: ['1', '6', '8(F)', '4(F)', '5'],
3: ['1', '8', '8', '1', '5']}
>>> df = pd.DataFrame(data=d)
>>> df
0 1 2 3
0 1 8(F) 1 1
1 2(F) 6 6 8
2 6 8 8(F) 8
3 8 4(F) 4(F) 1
4 5 4 5 5
And the result should look like this
0 2(F)
1 8(F)
2 8(F)
3 NaN
But when I used the code below, I received some errors
>>> mask = df.apply(lambda x: x.str.contains('F'))
>>> a = mask.idxmax().where(mask.any())
>>> print(df[a])
KeyError: '[nan] not in index'
Upvotes: 4
Views: 541
Reputation: 2022
Here's, a one-liner but it doesn't give out answer for 4th row:
df.replace("\d$", np.nan, regex=True).dropna(how='all', axis=1).apply(lambda x: x.dropna().iloc[0], 0)
It clears all elements other than \F, then for each column it finds out first non-empty elements.
Upvotes: 0
Reputation: 294218
applymap
, lookup
mask = df.applymap(lambda x: '(F)' in x)
vals = df[mask].lookup(mask.idxmax(), df.columns)
pd.Series(vals, df.columns)
0 2(F)
1 8(F)
2 8(F)
3 NaN
dtype: object
Over engineered
from numpy.core.defchararray import find
v = df.values.astype(str)
m = find(v, '(F)') >= 0
i = m.argmax(0)
j = np.arange(v.shape[1])
pd.Series(np.where(m[i, j], v[i, j], np.nan), df.columns)
Upvotes: 4
Reputation: 323226
Here is one way
mask = df.applymap(lambda x: '(F)' in x)
df[mask].bfill().iloc[0,]
Out[624]:
0 2(F)
1 8(F)
2 8(F)
3 NaN
Name: 0, dtype: object
Upvotes: 5
Reputation: 862511
Use numpy indexing for get values by idxmax
and last add where
:
mask = df.apply(lambda x: x.str.contains('F', na=False))
a = mask.idxmax()
s = pd.Series(df.values[a, a.index]).where(mask.any())
print(s)
0 2(F)
1 8(F)
2 8(F)
3 NaN
dtype: object
Another solution with reshape by DataFrame.stack
, filtering and get first value by GroupBy.first
, last add non exist values by Series.reindex
:
s = df.stack()
s = s[s.str.contains('F', na=False)].groupby(level=1).first().reindex(df.columns)
print (s)
0 2(F)
1 8(F)
2 8(F)
3 NaN
dtype: object
Upvotes: 4