Home > Enterprise >  Create New True/False Pandas Dataframe Column based on conditions
Create New True/False Pandas Dataframe Column based on conditions

Time:11-24

Year District Geometry TRUE/FALSE
1900 101 POLYGON ((-89.26355 41.32246, -89.26171 41.322... TRUE
1902 101 POLYGON ((-89.26355 41.33246, -89.26171 41.322... FALSE

I have a dataframe with a large number of columns and rows (only a sample above) and I am trying to create a new column with a conditional response, not based on values within the same row (all of the posts I have read so far seem to just refer to conditional column creation based on values in another column within the same row).

I want to compare the Geometry column, which is a GeometryArray datatype, with the same geometry column of the same district two years earlier.

Phrased as a question: Is the geometry of district 101 in 1902 the same as district 101 in 1900? TRUE/FALSE

df['geometry change from last year'] = np.where(df['geometry'].at[df.index[i]]!= climate[geometry].at[df.index[i-2]], 'True', 'False')

CodePudding user response:

Depending on how your rows are actually organized, you could use eq together with a shift.

(partial answer from here)

First create the dummy dataframe:

import pandas as pd

data = {'Year':[1900,1901,1902],
        'District':[101,101,101],
        'Geometry':[
             'POLYGON ((-89.26355 41.32246, -89.26171 41.322))',
             'POLYGON ((-89.26355 41.33246, -89.26171 41.322))',
             'POLYGON ((-89.26255 41.33246, -89.26171 41.322))'],
        }

df = pd.DataFrame(data)
df

The dataframe looks like:

   Year  District                                          Geometry
0  1900       101  POLYGON ((-89.26355 41.32246, -89.26171 41.322))
1  1901       101  POLYGON ((-89.26355 41.33246, -89.26171 41.322))
2  1902       101  POLYGON ((-89.26355 41.33246, -89.26171 41.322))

Then, combining the mentionned functions:

df['changed'] = df['Geometry'].eq(df['Geometry'].shift(2).bfill().astype(bool)
df

outputs:

   Year  District                                          Geometry  changed
0  1900       101  POLYGON ((-89.26355 41.32246, -89.26171 41.322))    False
1  1901       101  POLYGON ((-89.26355 41.33246, -89.26171 41.322))     True
2  1902       101  POLYGON ((-89.26355 41.33246, -89.26171 41.322))     True

Though you would have to take a look at the very first two rows because of the bfill(), needed for the comparison.

  • Related