-7

I'm new to Python. Can anybody break down the process here per line? What does the [1:4] do? Thank you.

def answer_six(): 
    copy_df = census_df.copy()
    copy_df = copy_df.groupby(['STNAME']) 
    states_pop = pd.DataFrame(columns=['pop'])
    for i, c in copy_df: 
        states_pop.loc[i] = [c.sort_values(by='CENSUS2010POP', ascending=False)[1:4]['CENSUS2010POP'].sum()]
        top3 = states_pop.nlargest(3,'pop') 
    return states_pop answer_six()

1 Answers1

1

[1:4] slices the list or a tuple object. It means that it will generate new list from the original one by getting items of the list oject from index 1 to index 4.

Note that the first item of the original list and items after index 4 (fifth item will be included in the new generated list) are removed.

Jakub Bláha
  • 1,491
  • 5
  • 22
  • 43