简体   繁体   English

Pandas替换DataFrame中的第一个结果

[英]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 : 这是一个带有groupbyidxmin的矢量化解决方案:

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.

相关问题 将 pandas 数据框列中的每个值与第二个数据框列的所有值相乘并将每个第一个数据框值替换为结果数组 - Multiply each value in a pandas dataframe column with all values of 2nd dataframe column & replace each 1st dataframe value with resulting array 带有datetime.timedelta的Python / Pandas DataFrame第一行问题 - Python/Pandas DataFrame 1st line issue with datetime.timedelta 访问包含列表的 Pandas DataFrame 列的每个第一个元素 - Accessing every 1st element of Pandas DataFrame column containing lists 根据熊猫中的条件更改数据框的第一行 - Change 1st row of a dataframe based on a condition in pandas 我希望第二个 dataframe 中的行替换第一个 dataframe 中具有相同唯一 ID 的相应行 - I want the rows in 2nd dataframe to replace corresponding rows with same unique ID in the 1st dataframe 熊猫:在第一个数据框中获取行,在另一个数据框中使用相同的值(两列) - Pandas: Get Rows in 1st dataframe with same values (in two columns) in another dataframe 如何仅用逗号前第一个出现的单词替换 dataframe 文本列 - How to replace dataframe text column with only the 1st occuring word / words before a comma 用数据框中的“第 2 天”和“第 1 天”替换一列中的最后 2 个日期,以使代码动态化 - replace the last 2 dates in one column by "2nd day" and "1st day" in a dataframe to make the code dynamic 如何在pandas数据帧中过滤属于特定列的第1和第3四分位数的行? - How to filter rows that fall within 1st and 3rd quartile of a particular column in pandas dataframe? 如何强制 pandas dataframe 的第 2 级加起来达到第 1 级? - How to enforce 2nd level of pandas dataframe to add up to 1st level?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM