gpt4 book ai didi

python - 应用具有两个数据框字段的函数

转载 作者:行者123 更新时间:2023-12-02 11:31:18 27 4
gpt4 key购买 nike

我有一个像这样的数据框(创建示例数据框);

df = pd.DataFrame({'language': ['ruby','ruby','ruby', np.nan,'ruby'],
'top_lang_owned': [['ruby', 'javascript', 'go'],
['ruby', 'coffeescript'],
['javascript', 'coffeescript'],
['ruby', 'shell', 'go'],
np.nan],
'top_lang_watched': [['ruby','go'],
['javascript'],
np.NaN,
['ruby', 'shell'],
np.nan]})
df
  language    top_lang_owned          top_lang_watched
0 ruby [ruby, javascript, go] [ruby, go]
1 ruby [ruby, coffeescript] [javascript]
2 ruby [javascript, coffeescript] NaN
3 NaN [ruby, shell, go] [ruby, shell]
4 ruby NaN NaN
dataframe.info();
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
language 4 non-null object
top_lang_owned 4 non-null object
dtypes: object(2)
memory usage: 208.0+ bytes

我想添加一个比较两个字段值的字段。 (伪代码)

if ("language" is in "top_lang_owned") 
then new_field = 1 othervise new_field = 0.

例如,所需的输出必须如下;

language  top_lang_owned           top_lang_watched  is_owned is_watched
0 ruby [ruby, javascript, go] [ruby, go] 1 1
1 ruby [ruby, coffeescript] [javascript] 1 0
2 ruby [javascript, coffeescript] NaN 0 0
3 NaN [ruby, shell, go] [ruby, shell] NaN NaN
4 ruby NaN NaN NaN NaN

最佳答案

你当然可以做到,这是您可能想尝试的代码,

编辑:

def func(x):
if x.language in x.top_lang_owned:
return 1
return 0

df['is_in_lang'] = df[~df.isna().any(1)].apply(func, axis=1)

输出:

    id language                    top_lang_owned  is_in_lang
0 21 ruby [ruby, javascript, go] 1
1 25 ruby [javascript, ruby, coffeescript] 1
2 38 ruby [javascript, coffeescript] 0
3 108 NaN [ruby, shell, go] NaN
4 173 ruby NaN NaN

关于python - 应用具有两个数据框字段的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60395408/

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