0

A sample loop as below, how can I name the sub_df to be sub_df_1, sub_df_2 .... based on the loop number?

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/justmarkham/pandas-videos/master/data/imdb_1000.csv')

split_point = [0,10,25,168]
n =len(split_point) 
for i in  range (0,n-1): 
  print("Dataset ", i, " - Record Range: ",  split_point[i], '-', split_point[i+1]-1   )
  sub_df = df.iloc[ split_point[i]:split_point[i+1] , : ]
bigbounty
  • 16,526
  • 5
  • 37
  • 65

1 Answers1

0

Name the sub-dataframes in a dictionary as follows:

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/justmarkham/pandas-videos/master/data/imdb_1000.csv')

split_point = [0,10,25,168]
n =len(split_point) 

dataframes = {}
for i in  range (0,n-1): 
  dataframes[f'sub_df_{i}'] = df.iloc[ split_point[i]:split_point[i+1] , : ]

#for example, to access sub_df_1:
print(dataframes['sub_df_1'])
pakpe
  • 5,391
  • 2
  • 8
  • 23