2

I have a pandas dataframe and I want to iterate over rows of this dataframe, get slices of data, based on a value in a column.

To say it more brief, I have a dataframe like below:

districts = [['dist','name','sale','purchase'],['dis1','avelin',2300, 1400],['dis2','matri', 4300, 2500], ['dis1', 'texi', 1500, 1700],['dis2','timi', 2300, 1400]]

I'd like to iterate over all rows and extract dataframes based on 'dist' column.
the output should look like below:

dis1 = [[2300, 1400], [1500,1700]]
dis2 = [[4300,2500],[2300,1400]]  
1
  • Have a look here to learn how to format your question so it is readable Commented Jul 9, 2019 at 11:12

1 Answer 1

1

As a preface, you aren't really working with pandas as you currently have your code set up. You have a list of lists, but it is not a pandas dataframe. To actually work with pandas:

districts = [['dis1','avelin',2300, 1400],
             ['dis2','matri', 4300, 2500],
             ['dis1', 'texi', 1500, 1700],
             ['dis2','timi', 2300, 1400]]
df = pd.DataFrame(data=districts, columns=['dist','name','sale','purchase'])

From there, the process of subsetting data frames is easy -- 'iteration' is not needed (and rarely is when working with pandas):

dis1 = df.loc[df['dist'] == 'dis1']
dis2 = df.loc[df['dist'] == 'dis2']

This gives the result:

   dist    name  sale  purchase
0  dis1  avelin  2300      1400
2  dis1    texi  1500      1700

   dist   name  sale  purchase
1  dis2  matri  4300      2500
3  dis2   timi  2300      1400

If you haven't already, you should read through the pandas help pages -- e.g., the Getting Started and Indexing and Selecting Data pages.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.That was useful. However, my dataset has almost 20 districts and I've just provided some of it. I don't want to define each district manually. Moreover, I would like that the code detect the unique districts and then subtract my desired columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.