Say I want to run any piece of code I have written (a simple example below):
df_VisitorType_no = pd.DataFrame(columns=['VisitorType_no'])
for i in range(df.shape[0]):
if df.loc[i,'VisitorType'] == 'Returning_Visitor':
df_VisitorType_no.loc[i,'VisitorType_no'] = 1
elif df.loc[i,'VisitorType'] == 'New_Visitor':
df_VisitorType_no.loc[i,'VisitorType_no'] = 2
else:
df_VisitorType_no.loc[i,'VisitorType_no'] = 3
My dataframe df
has a huge number of rows and I want to first test out the code I have written to see if I had written it correctly by not running on all the rows in df
but just a select few (say first 100 rows) so that I could quickly check the code I have written works correctly.
Instead of doing this:
df = df[0:100,:]
df_VisitorType_no = pd.DataFrame(columns=['VisitorType_no'])
for i in range(df.shape[0]):
if df.loc[i,'VisitorType'] == 'Returning_Visitor':
df_VisitorType_no.loc[i,'VisitorType_no'] = 1
elif df.loc[i,'VisitorType'] == 'New_Visitor':
df_VisitorType_no.loc[i,'VisitorType_no'] = 2
else:
df_VisitorType_no.loc[i,'VisitorType_no'] = 3
Is there a way in Python where I could just specify something like n_rows = 100
at the top of the code, ie. something like this?:
n_rows = 100
df_VisitorType_no = pd.DataFrame(columns=['VisitorType_no'])
for i in range(df.shape[0]):
if df.loc[i,'VisitorType'] == 'Returning_Visitor':
df_VisitorType_no.loc[i,'VisitorType_no'] = 1
elif df.loc[i,'VisitorType'] == 'New_Visitor':
df_VisitorType_no.loc[i,'VisitorType_no'] = 2
else:
df_VisitorType_no.loc[i,'VisitorType_no'] = 3
My question would also apply to arrays if there is a way to do this in both dataframes and arrays. Many thanks.