Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15851

Python dataframe Selecting specific number of rows from column 1 and all rows from column 2

$
0
0

I have two columns in excel sheet that looks like this

enter image description here

I want to iterate in python (using dataframe if possible) to produce multiple excel sheets where my excel files would look like this

File_1 Output (Column 1 will get 10 cities, column 2 will have all persons)

enter image description here

File_2 Output (Column 1 will get the next 10 cities, column 2 will have all persons without any change)

enter image description here

The for loop will continue to iterate until all the rows are done.Let's say if I have 105 cities in column 1, the code will generate 11 output files where the last file will only have 5 cities in column 1.

I have tried to use the following code

rows_per_file = 10n_chunks = PC // rows_per_filefor i in range(n_chunks):    start = i*rows_per_file    stop = (i+1) * rows_per_file    sub_df= df(df.iloc[start:stop,0],0)    sub_df2=df(df.iloc[0:UC,1],1)    sub_df3= sub_df+sub_df2    sub_df3.to_excel(f"test-{i}.xlsx", sheet_name="sheet")

But it gives me all data in single column and slices my 2nd column to 10 person too instead of all 26 person.I have tried different ways but haven't been able to figure out.

Any help is appreciated :)

enter image description here


Viewing all articles
Browse latest Browse all 15851

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>