简体   繁体   中英

Create New True/False Pandas Dataframe Column based on conditions

Year District Geometry TRUE/FALSE
1900 101 POLYGON ((-89.26355 41.32246, -89.26171 41.322... TRUE
1902 101 POLYGON ((-89.26355 41.3 3 246, -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')

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM