An example of the PANDAS data I'm working with looks like this (bellow) where A-F have certain attributes 0-3, 3 being a value of most suitable and 0 not suitable. I want to find in each ID is if there are multiple high suitability columns within each row. If there are more than one in a row with the same max value I want to name them in a list in a new column of this data frame called 'Conflicts'. This is how I have tried to achieve that so far however has not been successful
ID A B C D E F max_value
0 0.0 0.0 0.0 1.0 0.0 0.0 1.0
1 3.0 0.0 0.0 1.0 1.0 2.0 3.0
2 3.0 1.0 0.0 1.0 1.0 2.0 3.0
3 0.0 1.0 0.0 3.0 3.0 2.0 3.0
I am currently trying to use Python to produce a list of column names with the same max value then put it as a list in a column called Conflicts in my dataframe.
#Making a blank dataframe to put in the column names that
have #the same maximum value in a row
df["Conflict"]= ""
df["Conflict"]=df["Conflict"].astype('object')
for row in df:
if row==[df.A == df.max_value]:
df["Conflict"].append("a")
elif row==[df.B == df.max_value]:
df["Conflict"].append("b")
elif row==[df.C == df.max_value]:
df["Conflict"].append("c")
elif row ==[df.D == df.max_value]:
df["Conflict"].append("d")
elif row ==[df.E == df.max_value]:
df["Conflict"].append("e")
elif row ==[df.F == df.max_value]:
df["Conflict"].append("f")
else:
df["Conflict"]==("NA")
I get this error TypeError: cannot concatenate object of type ""; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid
This is my desired output:
ID A B C D E F max_value Conflicts
0 0.0 0.0 0.0 1.0 0.0 0.0 1.0 NA
1 3.0 0.0 0.0 1.0 1.0 2.0 3.0 NA
2 3.0 1.0 0.0 1.0 1.0 2.0 3.0 NA
3 0.0 1.0 0.0 3.0 3.0 2.0 3.0 d,e
I'm new to python so any help would be greatly appreciated! Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire