[英]Pandas Replace 1st Result in a DataFrame
Let's say I have a dataframe that looks like this: 假设我有一个如下所示的数据框:
df4
df4 = pd.DataFrame({'Q':['apple', 'apple', 'orange', 'Apple', 'orange'], 'R':['a.txt', 'a.txt', 'a.txt', 'b.txt', 'b.txt']})
>>> df4
Q R
0 apple a.txt
1 apple a.txt
2 orange a.txt
3 Apple b.txt
4 orange b.txt
What I would like to output is this: 我想输出的是:
Q R
0 breakfast a.txt
1 apple a.txt
2 orange a.txt
3 breakfast b.txt
4 orange b.txt
In other words, case insensitive, I want to search every row in a dataframe, find the first occurrence of certain words (in this case, that word is apple), and replace it with another word. 换句话说,不区分大小写,我想搜索数据帧中的每一行,找到某些单词的第一个出现(在这种情况下,该单词是apple),并将其替换为另一个单词。
Is there a way to do this? 有没有办法做到这一点?
Here's a vectorised solution with groupby
and idxmin
: 这是一个带有groupby
和idxmin
的矢量化解决方案:
v = df.Q.str.lower().eq('apple')
v2 = (~v).cumsum().where(v)
df.loc[v2.groupby(v2).idxmin().values, 'Q'] = 'breakfast'
df
Q R
0 breakfast a.txt
1 apple a.txt
2 orange a.txt
3 breakfast b.txt
4 orange b.txt
I just really wanted to answer this question. 我真的很想回答这个问题。
def swap_first(s):
swap = 1
luk4 = {'apple'}
for x in s:
if x.lower() in luk4 and swap:
yield 'breakfast'
swap ^= 1
else:
yield x
if x not in luk4:
swap ^= 1
df4.assign(Q=[*swap_first(df4.Q)])
Q R
0 breakfast a.txt
1 apple a.txt
2 orange a.txt
3 breakfast b.txt
4 orange b.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.