Search code examples
pythonpandasdataframeslicegeopy

How to properly slice this dataframe?


I have a dataframe with 18000 rows and I want to slice it in 18 dataframes. I am doing this:

p1 = df[0:1000]
p2 = df[1001:2001]
p3 = df[2002:3002]
p4 = df[3003:4003]
p5 = df[4004:5004]
p6 = df[5005:6005]
p7 = df[6006:7006]
p8 = df[7007:8007]
p9 = df[8008:9008]
p10 = df[9009:10009]
p11 = df[10013:11013]
p12 = df[11014:12014]
p13 = df[12015:13015]
p14 = df[13016:14016]
p15 = df[14017:15017]
p16 = df[15018:16018]
p17 = df[16019:17019]
p18 = df[17020:18020]

Is any other way of doing this but more efficiently?

I am using pandas and geopy because I want to look for addresses but since geopy has a limit requests per day I am going to put each dataframe in different notebooks so like this I might be able to run it in differents computers.


Solution

  • Use numpy.array_split():

    In [4]: import numpy as np
    In [5]: np.array_split(df, 18)
    

    This will return you a list with 18 elements, each in itself in a df.