0

How do divide pandas dataframe in other blocks of data?

Attention: I don't want to do multiple queries to the database. I already have the query result in the desired size. What I need is to break the resulting dataframe into smaller dataframes.

I am start learn to work with pandas and try make a division of datas from a dataframe. I connect to SQL Server using pyodbc and get data with pandas.read_sql(...). The result is aproximately 5000 records. I need fragment the dataframe in blocks of 50 records without a loop (while, for, etc). I am convert the dataframe in dictionary and I am trying use compreension, but I don't have success:

[x for x in original_dataframe[:int(len(original_dataframe)/50)]

I need a result a blocks of other dicts or dataframes, e.g.:

block_01 = original_5000_records[:50]
block_02 = original_5000_records[50:100]

etc.

I yet do not understand weel, but I believe that is possible to use pandas.shape() or pandas.iloc(), maybe there are the way. Someone can helpe me to make and compreend this? Thanks.

3
  • Use chunksize parameter in pd.read_sql Commented Aug 26, 2020 at 13:30
  • I don't want to do multiple queries to the database. I already have the query result in the desired size. What I need is to break the resulting dataframe into smaller dataframes. Commented Aug 26, 2020 at 14:07
  • Resolved using numpy method "array_split()" Commented Aug 26, 2020 at 16:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.