Skip to main content
added 98 characters in body
Source Link
Bera
  • 77.8k
  • 14
  • 78
  • 187

See: Missing data, insert rows in Pandas and fill with NAN

import geopandas as gpd
import pandas as pd
df = pd.DataFrame(gpd.read_file(r'C:\GIS\data\testdata\years.shp'))

#My dataframe have 3 rows/years: 2001, 2005, 2021:
#   loss_year   area  prc                                           geometry
#0       2001  42524    1  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
#1       2005      1    1  POLYGON ((15.28429 58.93355, 15.28690 59.24807...
#2       2021    111    1  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

df = df.set_index("loss_year").reindex(pd.Index(range(2001,2022,1), name="loss_year")).reset_index()
df = df[['loss_year','area','prc']].fillna(0) 

#Now all the years from 2001-2021 are there:
# print(df.head(3))
#    loss_year     area  prc
# 0       2001  42524.0  1.0
# 1       2002      0.0  0.0
# 2       2003      0.0  0.0
# print(df.tail(3))
# 18       2019    0.0  0.0
# 19       2020    0.0  0.0
# 20       2021  111.0  1.0

See: Missing data, insert rows in Pandas and fill with NAN

import geopandas as gpd
import pandas as pd
df = pd.DataFrame(gpd.read_file(r'C:\GIS\data\testdata\years.shp'))

#   loss_year   area  prc                                           geometry
#0       2001  42524    1  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
#1       2005      1    1  POLYGON ((15.28429 58.93355, 15.28690 59.24807...
#2       2021    111    1  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

df = df.set_index("loss_year").reindex(pd.Index(range(2001,2022,1), name="loss_year")).reset_index()
df = df[['loss_year','area','prc']].fillna(0)

# print(df.head(3))
#    loss_year     area  prc
# 0       2001  42524.0  1.0
# 1       2002      0.0  0.0
# 2       2003      0.0  0.0
# print(df.tail(3))
# 18       2019    0.0  0.0
# 19       2020    0.0  0.0
# 20       2021  111.0  1.0

See: Missing data, insert rows in Pandas and fill with NAN

import geopandas as gpd
import pandas as pd
df = pd.DataFrame(gpd.read_file(r'C:\GIS\data\testdata\years.shp'))

#My dataframe have 3 rows/years: 2001, 2005, 2021:
#   loss_year   area  prc                                           geometry
#0       2001  42524    1  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
#1       2005      1    1  POLYGON ((15.28429 58.93355, 15.28690 59.24807...
#2       2021    111    1  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

df = df.set_index("loss_year").reindex(pd.Index(range(2001,2022,1), name="loss_year")).reset_index()
df = df[['loss_year','area','prc']].fillna(0) 

#Now all the years from 2001-2021 are there:
# print(df.head(3))
#    loss_year     area  prc
# 0       2001  42524.0  1.0
# 1       2002      0.0  0.0
# 2       2003      0.0  0.0
# print(df.tail(3))
# 18       2019    0.0  0.0
# 19       2020    0.0  0.0
# 20       2021  111.0  1.0
deleted 187 characters in body
Source Link
Bera
  • 77.8k
  • 14
  • 78
  • 187

See: Missing data, insert rows in Pandas and fill with NAN

import geopandas as gpd
import pandas as pd
 
df = pd.DataFrame(gpd.read_file(r'C:\GIS\data\testdata\years.shp'))

#   loss_year   area  prc                                           geometry
#0       2001  42524    1  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
#1       2005      1    1  POLYGON ((15.28429 58.93355, 15.28690 59.24807...
#2       2021    111    1  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

df = df.set_index("loss_year").reindex(pd.Index(range(2001,2022,1), name="loss_year")).reset_index()
df = df[['loss_year','area','prc']].fillna(0)

# print(df.head(3))
#    loss_year     area  prc                                           geometry
# 0       2001  42524.0  1.0  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
# 1       2002      NaN  NaN                             0.0  0.0
# 2       2003      0.0  None0.0
# ..print(df.tail(3))
#18       2019    NaN  NaN                                 # 18       2019    0.0  None0.0
#19       2020    NaN  NaN                                 # 19       2020    0.0  None0.0
#20# 20       2021  111.0  1.0  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

See: Missing data, insert rows in Pandas and fill with NAN

import geopandas as gpd
import pandas as pd
 
df = pd.DataFrame(gpd.read_file(r'C:\GIS\data\testdata\years.shp'))

#   loss_year   area  prc                                           geometry
#0       2001  42524    1  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
#1       2005      1    1  POLYGON ((15.28429 58.93355, 15.28690 59.24807...
#2       2021    111    1  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

df = df.set_index("loss_year").reindex(pd.Index(range(2001,2022,1), name="loss_year")).reset_index()

#    loss_year     area  prc                                           geometry
# 0       2001  42524.0  1.0  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
# 1       2002      NaN  NaN                                               None
# ...
#18       2019    NaN  NaN                                               None
#19       2020    NaN  NaN                                               None
#20       2021  111.0  1.0  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

See: Missing data, insert rows in Pandas and fill with NAN

import geopandas as gpd
import pandas as pd
df = pd.DataFrame(gpd.read_file(r'C:\GIS\data\testdata\years.shp'))

#   loss_year   area  prc                                           geometry
#0       2001  42524    1  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
#1       2005      1    1  POLYGON ((15.28429 58.93355, 15.28690 59.24807...
#2       2021    111    1  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

df = df.set_index("loss_year").reindex(pd.Index(range(2001,2022,1), name="loss_year")).reset_index()
df = df[['loss_year','area','prc']].fillna(0)

# print(df.head(3))
#    loss_year     area  prc
# 0       2001  42524.0  1.0
# 1       2002      0.0  0.0
# 2       2003      0.0  0.0
# print(df.tail(3))
# 18       2019    0.0  0.0
# 19       2020    0.0  0.0
# 20       2021  111.0  1.0
Source Link
Bera
  • 77.8k
  • 14
  • 78
  • 187

See: Missing data, insert rows in Pandas and fill with NAN

import geopandas as gpd
import pandas as pd

df = pd.DataFrame(gpd.read_file(r'C:\GIS\data\testdata\years.shp'))

#   loss_year   area  prc                                           geometry
#0       2001  42524    1  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
#1       2005      1    1  POLYGON ((15.28429 58.93355, 15.28690 59.24807...
#2       2021    111    1  POLYGON ((14.62025 58.23570, 14.61672 58.56270...

df = df.set_index("loss_year").reindex(pd.Index(range(2001,2022,1), name="loss_year")).reset_index()

#    loss_year     area  prc                                           geometry
# 0       2001  42524.0  1.0  POLYGON ((14.71964 58.73581, 14.71703 59.05656...
# 1       2002      NaN  NaN                                               None
# ...
#18       2019    NaN  NaN                                               None
#19       2020    NaN  NaN                                               None
#20       2021  111.0  1.0  POLYGON ((14.62025 58.23570, 14.61672 58.56270...