0

I'm struggling to get my head around the best way to apply if type logic to pandas dataframes.

What i think i keep doing is trying to apply the logic i'm providing to the array as a whole, rather than the values within the array row by row. But i'm also trying to do it without list comprehension or some kind of loop as it will be a few different scenarios i'm trying to account for...

I'm trying to populate a new field df['Summary Table'] that i can then groupby etc later for different aggregations.

The first thing i tried was like this


tbl_scenarios =[

    # Scenario 1: The town in a row is on the town list
    ((df['Town'] in towns) == True),

    # Scenario 2: If the value in the town column is 'Rural'
    (df['Town'] == 'Rural')

]

Below is my list of possible results. I want it to just return the value it finds in df['Town'] if it's on the list for Scenario 1, and if Scenario 2 is true i want it to return the value in the 'Area' column with the suffix ": 'Rural"

tbl_results = [df['Town'], str(df['CNA']) + ": Rural"]

Below i'm trying to select the right result from the list of results based on the scenario

df['Summary Table'] = np.select(tbl_scenarios, tbl_results, default=0)

I'm getting a value error for the above saying the truth is ambiguous (because it's looking at the whole array). Then i tried the below as a argument in the list:

(np.where(df['Town'])in towns)

but looking at the table, it doesn't look like this logic is working. Any ideas?

GlassShark1
  • 153
  • 1
  • 13
  • I'm confused. What are your conditions? What exactly is the expected output? – Mohit Motwani Feb 27 '20 at 11:12
  • I think problem is with `(df['Town'] in towns`, need `isin` – jezrael Feb 27 '20 at 11:14
  • expected output would be a new column, where if the town in the town row in the dataframe is in the town list, the column would just say the town, but if the town column says 'rural' for a row, it should take the value of another column (Area) and concatenate the string value with ':Rural' – GlassShark1 Feb 27 '20 at 11:30

0 Answers0