Home > Mobile >  Append function does not add anything to the panda dataframe
Append function does not add anything to the panda dataframe

Time:11-05

I have the following code:

import arcpy
from arcpy import env
import pandas as pd

#setting workspace
env.workspace = r"C:\Users\4_projects\211104\Network.gdb"
env.overwriteOutput = True  #existing output will be overwritten
buchs = r"buchs"

rows = arcpy.SearchCursor(buchs)
shapeName = arcpy.Describe(buchs).shapeFieldName

df1 = pd.DataFrame()

for row in rows:
    feat = row.getValue(shapeName)
    extent = feat.extent
    df2 = pd.DataFrame({"XMin": [extent.XMin],
                "YMin": [extent.YMin],
                "XMax": [extent.XMax],
                "YMax": [extent.YMax]})
    df1.append(df2, ignore_index=True)

My goal is to extract from every row the X and Y extent by creating a temporary df df2 and then append this to df1. So df1 is supposed to have row by row, all the extents of every entry. However, my df1always results in an empty df.

Any help is appreciated. If additional data is necessary, please let me know how to best share a shapefile here, thank you.

CodePudding user response:

Pandas DataFrame.append() has to be assigned to a variable, otherwise it does append the other dataframe, but never assigns it and it's gone as soon as the appending finishes, so just add df1 = in front of the append:

    df1 = df1.append(df2, ignore_index=True)

CodePudding user response:

A possible solution related to my comment above:

data = []
for row in rows:
    feat = row.getValue(shapeName)
    extent = feat.extent
    df2 = pd.DataFrame({"XMin": [extent.XMin],
                "YMin": [extent.YMin],
                "XMax": [extent.XMax],
                "YMax": [extent.YMax]})
    data.append(df2)

df1 = pd.concat(data, ignore_index=True)

CodePudding user response:

instead of

df1.append(df2, ignore_index=True)

you can use

df1 = pd.concat([df1, df2])

if all the columns are matched

  • Related