0

Here is my example data frame:

|   Cat     |   Dog     |  Bird     |   Cow     |   Pig     |  
|:------:   |:------:   |:-----:    |:------:   |:------:   |  
|   Big     |   Big     | Small     | Medium    | Medium    |  
|  Small    |  Small    | Small     |  Small    |  Small    |  
| Medium    | Medium    | Small     | Medium    |  Small    |  

I would like to create another column 'Size' with the value 'Big' if any of the animals are Big or else 'Medium' if any are Medium or else 'Small' if all the animals are only Small. The result Im looking for is seen below.

|   Cat     |   Dog     |  Bird     |   Cow     |   Pig     | Size      |  
|:------:   |:------:   |:-----:    |:------:   |:------:   |--------   |  
|   Big     |   Big     | Small     | Medium    | Medium    | Big       |  
|  Small    |  Small    | Small     |  Small    |  Small    | Small     |  
| Medium    | Medium    | Small     | Medium    |  Small    | Medium    | 

I've tried

df['Size'] = df[['Cat', 'Dog', 'Bird', 'Cow','Pig' ].map(lambda x: 'Big' if 'Big' in x
                                                        else 'Medium' if 'Medium' in x 
                                                        else 'Small' if 'Small' in x)

I've also tried this and just added done it for all animals but it obviously only returns the results for the last one.

df['Size'] = df['Cat'].map(lambda x: 'Big' if 'Big' in x
                                                        else 'Medium' if 'Medium' in x
                                                        else 'Small' if 'Small' in x)
big_soapy
  • 137
  • 7
  • Please see [this question](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) on how to ask questions involving pandas dataframes. – vlizana Jun 26 '20 at 03:59

2 Answers2

0

You could try this:

df['Size']=['Big' if 'Big' in row else('Medium' if 'Medium' in row else('Small' if 'Small' in row else ''))for row in df.to_records()]
MrNobody33
  • 6,413
  • 7
  • 19
0

Create a series of checks according to your conditions, then apply numpy select to create your new column

cond1 = df.eq("Big").any(axis=1)
cond2 = df.eq("Medium").any(axis=1)
cond3 = df.eq("Small").all(axis=1)
condlist = [cond1, cond2, cond3]
choicelist = ["Big", "Medium", "Small"]
df["Size"] = np.select(condlist, choicelist)
sammywemmy
  • 27,093
  • 4
  • 17
  • 31