验证数值

huangapple go评论62阅读模式
英文:

Validating values

问题

我有一个存储的值列表,我需要将每个值与数据框中的每个列单元格进行比较,如果找到它,然后在Key列中放置一个值 "YES":

dep_list = df_epl[['Values']].copy()
dep_list = dep_list.dropna()
dep_list = dep_list.drop_duplicates()
dlist = dep_list['Values'].tolist()

df_existing['Key'] = ""
for l in dlist:
    for index, row in df_existing.iterrows():
        if row['Code'] == l:
            row['Key'] = "YES"
        else:
            pass

一切都正常工作,除了将 "YES" 值写入Key列时。我尝试了很多方法,但它就是不起作用。

英文:

I have a list of values stored, I need to compare each value with each column cell in a data frame, if its found then place a value of "YES" in a Key column:

dep_list = df_epl[['Values']].copy()
dep_list = dep_list.dropna()
dep_list = dep_list.drop_duplicates()
dlist = dep_list['Values'].tolist()

df_existing['Key'] = ""
for l in dlist:
    for index, row in df_existing.iterrows():
        if row['Code'] == l:
            row['Key'] = "YES"
        else:
            pass

Everything is working except when writing "YES" value into the Key column. I have tried a lot of ways and it just don't work.

答案1

得分: 1

在你的代码中,你正在将row['Key']l进行比较,而前者始终是"",因为它在for循环之前立即定义。

我猜你实际上想要写的是if row['Values'] == l

而使用pandas,你实际上可以使用以下方法来检查列值是否在列表中:

import pandas as pd

df1 = pd.DataFrame({'value': range(10)})

list_to_check = [1,2,3]

# 创建Key列
df1['Key'] = 'No'
df1.loc[df1['value'].isin(list_to_check), 'Key'] = 'YES'

df1

返回结果如下:

  value	Key
0 	0 	No
1 	1 	YES
2 	2 	YES
3 	3 	YES
4 	4 	No
5 	5 	No
6 	6 	No
7 	7 	No
8 	8 	No
9 	9 	No
英文:

In your code you are comparing row['Key'] to l, where the former is always "" as its defined immediately before the for loop.

I am guessing you actually meant to write if row['Values'] == l?

And with pandas you can actually use the following to check if your column value is inside a list:

import pandas as pd

df1 = pd.DataFrame({'value': range(10)})

list_to_check = [1,2,3]

# create Key
df1['Key'] = 'No'
df1.loc[df1['value'].isin(list_to_check), 'Key'] = 'YES'

df1

Returns:

  value	Key
0 	0 	No
1 	1 	YES
2 	2 	YES
3 	3 	YES
4 	4 	No
5 	5 	No
6 	6 	No
7 	7 	No
8 	8 	No
9 	9 	No

答案2

得分: 1

pandas.apply应该可以正常工作

import pandas as pd

# 假设dlist是一个字符串列表
dlist = ["aa", "bb"]

df = pd.DataFrame(columns=["X", "Y", "Z"], data=[["a", "c", "b"], ["c", "bb", "aa"]])

print(df)

   X   Y   Z
0  a   c   b
1  c  bb  aa

df["key"] = ""

df["key"] = df.apply(lambda x: "Yes" if any([y in dlist for y in x.values]) else "", axis=1)

print(df)

   X   Y   Z  key
0  a   c   b     
1  c  bb  aa  Yes
英文:

pandas.apply should work fine

import pandas as pd

# assuming dlist is a list of strings
dlist = ["aa", "bb"]

df = pd.DataFrame(columns=["X","Y","Z"], data=[["a","c","b"],["c","bb","aa"]])

print(df)

   X   Y   Z
0  a   c   b
1  c  bb  aa

df["key"] = ""


df["key"] = df.apply(lambda x: "Yes" if any([y in dlist for y in x.values]) else "", axis=1)

print(df)

   X   Y   Z  key
0  a   c   b     
1  c  bb  aa  Yes

huangapple
  • 本文由 发表于 2023年3月4日 03:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定