Every line of 'pandas add column of zeros' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
234 def add_zeros(df, ctx): 235 """ Add values of zero where we believe appropriate.""" 236 cellsize = ctx["sz"] * 1000.0 237 newrows = [] 238 # loop over the grid looking for spots to add a zero 239 for y in np.arange(ctx["bnds2163"][1], ctx["bnds2163"][3], cellsize): 240 for x in np.arange(ctx["bnds2163"][0], ctx["bnds2163"][2], cellsize): 241 # search a 2x radius for any obs 242 poly = Polygon( 243 [ 244 [x - cellsize, y - cellsize], 245 [x - cellsize, y + cellsize], 246 [x + cellsize, y + cellsize], 247 [x + cellsize, y - cellsize], 248 ] 249 ) 250 df2 = df[df["geo"].within(poly)] 251 if df2.empty: 252 # Add a zero at this "point" 253 (lon, lat) = T2163_4326.transform(x, y) 254 if ctx["z"] != "no": 255 newrows.append( 256 { 257 "geo": Point(x, y), 258 "lon": lon, 259 "lat": lat, 260 "val": 0, 261 "nwsli": "Z%s" % (len(newrows) + 1,), 262 USEME: True, 263 "plotme": False, 264 "state": "Z", 265 } 266 ) 267 continue 268 # For this grid cell, remove any values 20% of the max 269 maxval = df.at[df2.index[0], "val"] 270 df.loc[df2[df2["val"] >= (maxval * 0.2)].index, USEME] = True 271 df.loc[df2[df2["val"] >= (maxval * 0.2)].index, "plotme"] = True 272 273 return pd.concat( 274 [df, GeoDataFrame(newrows, geometry="geo")], 275 ignore_index=True, 276 sort=False, 277 )
14 def add_column_numpy_array(array, new_col): 15 placeholder = np.ones(array.shape[0])[:, np.newaxis] 16 result = np.hstack((array, placeholder)) 17 18 if isinstance(new_col, np.ndarray): 19 assert array.shape[0] == new_col.shape[0], "input array row counts \ 20 must be the same. \ 21 Expected: {0}\ 22 Actual: {1}".format(array.shape[0], 23 new_col.shape[0]) 24 assert len(new_col.shape) <= 2, "new column must be 1D or 2D" 25 26 if len(new_col.shape) == 1: 27 new_col = new_col[:, np.newaxis] 28 return np.hstack((array, new_col)) 29 elif isinstance(new_col, list): 30 assert len(new_col) == array.shape[0], "input array row counts \ 31 must be the same. \ 32 Expected: {0}\ 33 Actual: {1}".format(len(array), 34 len(new_col)) 35 new_col = np.array(new_col) 36 assert len(new_col.shape) == 1, "list elements cannot be iterable" 37 new_col = new_col[:, np.newaxis] 38 return np.hstack((array, new_col)) 39 else: 40 placeholder = np.ones(array.shape[0])[:, np.newaxis] 41 result = np.hstack((array, placeholder)) 42 result[:, -1] = new_col 43 return result