mcagriardic
mcagriardic

Reputation: 71

Changing the categories in a column pandas?

I was experimenting with iter function on pandas.

1- I made a list from a pandas column.

in1:

df_area_code_iter = iter(df["Area Code"])
df_area_code_iter_list = list(df_area_code_iter)
df_area_code_iter_list

out_1:

['Area 1',
 'Area 2',
 'Area 1',
 ...
 'Area 0']

2- Then I wanted to iterate through the elements of the list to replace Area 0 with PASS THIS.

in_2:

new_column = []

for i in df_area_code_iter_list:
    if i == "Area 0":
       i == "PASS THIS"
    new_column.append(i)

new_column

out_2:

['Area 1',
 'Area 2',
 'Area 1',
 ...
 'Area 0']

I know there are other methods to replace the values in a column. However, I want to figure it out by converting the dataframe to a list and then iterating over the elements.

in_3:

df["Area Code"] = df["Area Code"].replace(to_replace ="Area 0", value = 
"PASS THIS")
df["Area Code"]

out_3:

0          Area 1
1          Area 2
2          Area 1
3          Area 1
4       PASS THIS
5       PASS THIS
6       PASS THIS
...     ....

As I said, I am experimenting and I cannot see any reason as to why the for loop at in_2 is not working.

Upvotes: 1

Views: 1038

Answers (1)

jpp
jpp

Reputation: 164683

The main problem is assignment requires =, not the equality operator ==.

You are forced to append to a new list since the variable i inside your loop is a scalar, not a reference pointing to an element in your original list. Instead, you can use enumerate and modify your existing list:

for idx, val in enumerate(df_area_code_iter_list):
    if val == "Area 0":
        df_area_code_iter_list[idx] = "PASS THIS"

Or, more Pythonic, use a list comprehension:

new_list = [x if x != 'Area 0' else 'PASS THIS' for x in df_area_code_iter_list]

Upvotes: 1

Related Questions