[英]A simple IF statement in Python 3.X Pandas not working
This is supposed to be a simple IF statement that is updating based on a condition but it is not working.这应该是一个简单的 IF 语句,它根据条件进行更新,但它不起作用。
Here is my code这是我的代码
df["Category"].fillna("999", inplace = True)
for index, row in df.iterrows():
if (str(row['Category']).strip()=="11"):
print(str(row['Category']).strip())
df["Category_Description"] = "Agriculture, Forestry, Fishing and Hunting"
elif (str(row['Category']).strip()=="21"):
df["Category_Description"] = "Mining, Quarrying, and Oil and Gas Extraction"
The print statement打印声明
print(str(row['Category']).strip())
is working fine but updates to the Category_Description column are not working.工作正常,但 Category_Description 列的更新不起作用。
The input data has the following codes输入数据有以下代码
Category Count of Records
48 17845
42 2024
99 1582
23 1058
54 1032
56 990
32 916
33 874
44 695
11 630
53 421
81 395
31 353
49 336
21 171
45 171
52 116
71 108
61 77
51 64
62 54
72 51
92 36
55 35
22 14
The update resulted in更新导致
Agriculture, Forestry, Fishing and Hunting 41183
Here is a small sample of the dataset and code on repl.it https://repl.it/@RamprasadRengan/SimpleIF#main.py When I run the code above with this data I still see the same issue.这是 repl.it https://repl.it/@RamprasadRengan/SimpleIF#main.py上的数据集和代码的一个小样本当我用这些数据运行上面的代码时,我仍然看到同样的问题。
What am I missing here?我在这里想念什么?
You are performing a row operation but applying a dataframe change in the "IF" statement.您正在执行行操作,但在“IF”语句中应用了 dataframe 更改。 This will apply the values to all the records.
这会将值应用于所有记录。
Try sometime like:尝试一下:
def get_category_for_record(rec):
if (str(row['Category']).strip()=="11"):
return "Agriculture, Forestry, Fishing and Hunting"
elif (str(row['Category']).strip()=="21"):
return "Mining, Quarrying, and Oil and Gas Extraction"
df["category"] = df.apply(get_category_for_record, axis = 1)
I think you want to add a column to the dataframe that maps category to a longer description.我认为您想在 dataframe 中添加一列,将类别映射到更长的描述。 As mentioned in the comments, assignment to a column affects the entire column.
如评论中所述,分配给列会影响整个列。 But if you use a list, each row in the column gets the corresponding value.
但是如果使用列表,则列中的每一行都会获得相应的值。
So use a dictionary to map name to description, build a list, and assign it.所以用字典来对 map 名字进行描述,建立一个列表,然后赋值。
import pandas as pd
category_map = {
"11":"Agriculture, Forestry, Fishing and Hunting",
"21":"Mining, Quarrying, and Oil and Gas Extraction"}
df = pd.DataFrame([["48", 17845],
[" 11 ", 88888],
["12", 33333],
["21", 999]],
columns=["category", "count of records"])
# cleanup category and add description
df["category"] = df["category"].str.strip()
df["Category_Description"] = [category_map.get(cat, "")
for cat in df["category"]]
# alternately....
#df.insert(2, "Category_Description",
# [category_map.get(cat, "") for cat in df["category"]])
print(df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.