I've DataFrame with a lot of columns for example A_XX, A_X12, ...,B_XX,C_YY etc. All coulmns are integer.
DataFrame - DF:
ID | A_XX | A_X12 | ... | B_XX | C_YY | ... |
---|---|---|---|---|---|---|
1 | -1 | 0 | ... | 24 | 56 | ... |
I need change value for all columns A_:.
I' ve:
list_var=DF.filter(regex='A_').columns.to_list()
for var in list_var:
DF[var '_V2']=[x.var if (x.var==1 and x.B_XX>0 and ... etc) else -100 for x in DF.itertuples()
but it's not work. For one variable example:
DF['A_XX' '_V2']=[x.A_XX if (x.A_XX==1 and x.B_XX>0) else -100 for x in DF.itertuples()
it is ok. Could You help me ? How do this for many variables ?
CodePudding user response:
The problem that I see is that, when you call itertuples
you get named tuples, and referencing with a variable becomes challenging:
import pandas as pd
# my test DataFrame
DF = pd.DataFrame({'A_1': {'0': "aaa", '1': "bbb", '2': "ccc"}, 'A_2': {'0': 0, '1': 6, '2': 0}, 'B_XX': {'0': 1, '1': 2, '2': 3}})
var = "A_1"
# [x.A_1 for x in DF.itertuples()] # This works
# [x.var for x in DF.itertuples()] # This does not work
# [x["var"] for x in DF.itertuples()] # This does not work
# [getattr(x, "var") for x in DF.itertuples()] # This works
So, if you want to keep your itertuples
you do like this:
for var in list_var:
DF[var '_V2']=[getattr(x, var) if (getattr(x, var) == 1 and getattr(x, "B_XX")>0) else -100 for x in DF.itertuples()]
or you use apply
for var in list_var:
DF[var '_V2']=DF.apply(lambda x: x[var] if (x[var] == 1 and x["B_XX"]>0) else -100, axis=1)