gpt4 book ai didi

Python:函数返回的值未在 Pandas 数据框中更新

转载 作者:行者123 更新时间:2023-12-03 19:39:50 26 4
gpt4 key购买 nike

我有一个 fruits带列的数据框:(Name, Color)和一个 sentence带列的数据框:(Sentence) .
水果数据框

          Name   Color
0 Apple Red
1 Mango Yellow
2 Grapes Green
3 Strawberry Pink
句子数据框
                      Sentence
0 I like Apple, Mango, Grapes
1 I like ripe Mango
2 Grapes are juicy
3 Oranges are citric
我需要将水果数据帧的每一行与句子数据帧的每一行进行比较,如果水果名称在句子中完全如此,则在句子中的水果名称之前连接它的颜色。
这就是我使用 dataframe.apply() 所做的:
import pandas as pd
import regex as re

# create fruit dataframe
fruit_data = [['Apple', 'Red'], ['Mango', 'Yellow'], ['Grapes', 'Green']]
fruit_df = pd.DataFrame(fruit_data, columns = ['Name', 'Color'])
print(fruit_df)

# create sentence dataframe
sentence = ['I like Apple, Mango, Grapes', 'I like ripe Mango', 'Grapes are juicy']
sentence_df = pd.DataFrame(sentence, columns = ['Sentence'])
print(sentence_df)


def search(desc, name, color):

if re.findall(r"\b" + name + r"\b", desc):

# for loop is used because fruit can appear more than once in sentence
all_indexes = []
for match in re.finditer(r"\b" + name + r"\b", desc):
all_indexes.append(match.start())

arr = list(desc)
for idx in sorted(all_indexes, reverse=True):
arr.insert(idx, color + " ")

new_desc = ''.join(arr)
return new_desc

def compare(name, color):
sentence_df['Result'] = sentence_df['Sentence'].apply(lambda x: search(x, name, color))


fruit_df.apply(lambda x: compare(x['Name'], x['Color']), axis=1)
print ("The final result is: ")
print(sentence_df['Result'])
我得到的结果是:
                      Sentence     Result
0 I like Apple, Mango, Grapes None
1 I like ripe Mango None
2 Grapes are juicy None
3 Oranges are citric None
预期结果:
                      Sentence                                        Result
0 I like Apple, Mango, Grapes I like Red Apple, Yellow Mango, Green Grapes
1 I like ripe Mango I like ripe Yellow Mango
2 Grapes are juicy Green Grapes are juicy
3 Oranges are citric
我也尝试遍历 fruits_df使用 itertuples()但结果还是一样
for row in fruit_df.itertuples():
result = sentence_df['Sentence'].apply(lambda x: search(x, getattr(row, 'Name'), getattr(row, 'Color')))
print(result)
我不明白为什么 search 返回的值函数未存储在新列中。这是正确的方法还是我错过了什么?

最佳答案

问题是你打电话compare每行 Fruit但在每次通过时使用相同的输入。
我刚刚添加了一些调试打印到 compare函数来了解发生了什么:

def compare(name, color):
print(name, color)
sentence_df['Result'] = sentence_df['Sentence'].apply(lambda x: search(x, name, color))
print(sentence_df['Result'])
并得到:
Apple Red
0 I like Red Apple, Mango, Grapes
1 None
2 None
Name: Result, dtype: object
Mango Yellow
0 I like Apple, Yellow Mango, Grapes
1 I like ripe Yellow Mango
2 None
Name: Result, dtype: object
Grapes Green
0 I like Apple, Mango, Green Grapes
1 None
2 Green Grapes are juicy
Name: Result, dtype: object
因此,当水果存在时,您成功添加了颜色,但如果不存在则返回 None,并且每次通过时都从原始列开始,因此只保留最后一个。
怎么修:
  • 先添加一个缺失的return desc在搜索中,避免 None结果
     def search(desc, name, color):

    if re.findall(r"\b" + name + r"\b", desc):
    ...
    new_desc = ''.join(arr)
    return new_desc
    return desc
  • 初始化 df['Result']在应用比较之前,并将其用作其输入:
     def compare(name, color):
    sentence_df['Result'] = sentence_df['Result'].apply(lambda x: search(x, name, color))

    sentence_df['Result'] = sentence_df['Sentence']
    fruit_df.apply(lambda x: compare(x['Name'], x['Color']), axis=1)

  • 最终达到预期:
    The final result is: 
    0 I like Red Apple, Yellow Mango, Green Grapes
    1 I like ripe Yellow Mango
    2 Green Grapes are juicy
    Name: Result, dtype: object

    关于Python:函数返回的值未在 Pandas 数据框中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66533183/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com