Andrew T
Andrew T

Reputation: 186

Unable to change value of dataframe at specific location

So I'm trying to go through my dataframe in pandas and if the value of two columns is equal to something, then I change a value in that location, here is a simplified version of the loop I've been using (I changed the values of the if/else function because the original used regex and stuff and was quite complicated):

pro_cr = ["IgA", "IgG", "IgE"] # CR's considered productive
rows_changed = 0
prod_to_unk = 0
unk_to_prod = 0

changed_ids = []

for index in df_sample.index:
    if num=1 and color="red":
        pass
    elif num=2 and color="blue":
        prod_to_unk += 1
        changed_ids.append(df_sample.loc[index, "Sequence ID"])
        df_sample.at[index, "Functionality"] = "unknown"
        rows_changed += 1
    elif num=3 and color="green":
        unk_to_prod += 1
        changed_ids.append(df_sample.loc[index, "Sequence ID"])
        df_sample.at[index, "Functionality"] = "productive"
        rows_changed += 1
    else:
        pass

print("Number of productive columns changed to unknown: {}".format(prod_to_unk))
print("Number of unknown columns changed to productive: {}".format(unk_to_prod))
print("Total number of rows changed: {}".format(rows_changed))

So the main problem is the changing code:

df_sample.at[index, "Functionality"] = "unknown" # or productive

If I run this code without these lines of code, it works properly, it finds all the correct locations, tells me how many were changed and what their ID's are, which I can use to validate with the CSV file.

If I use df_sample["Functionality"][index] = "unknown" # or productive the code runs, but checking the rows that have been changed shows that they were not changed at all.

When I use df.at[row, column] = value I get "AttributeError: 'BlockManager' object has no attribute 'T'"

I have no idea why this is showing up. There are no duplicate columns. Hope this was clear (if not let me know and I'll try to clarify it). Thanks!

Upvotes: 2

Views: 194

Answers (2)

Lawrence Khan
Lawrence Khan

Reputation: 62

You can also iat.

Example: df.iat[iTH row, jTH column]

Upvotes: 0

s3dev
s3dev

Reputation: 9701

To be honest, I've never used df.at - but try using df.loc instead:

df_sample.loc[index, "Functionality"] = "unknown"

Upvotes: 2

Related Questions