2

I wrote a function that calculates the projected population per year based on values in different columns (these columns are not shown for simplicity).

How do I append these rows to the dataframe?

import pandas as pd

data = {
'state': ['Ohio','New York'],
'year': [2000,2000],
'pop': [2.5,3.6]
}
census = pd.DataFrame(data)

def projected_pop_by_year(s):
    new_census = pd.DataFrame()
    current_pop = census[census['state'] == s]['pop'].values[0]
    current_year = census[census['state'] == s]['year'].values[0]
    i = 0; count = 1
    while (i + 1) <= current_pop:
        projected_pop = None # some calculations
        data = {
            'state' : [s],
            'year' :  [current_year + count],
            'pop': [projected_pop]
        }
        print((pd.DataFrame(data)))
        i += 1; count += 1


projected_pop_by_year("Ohio")

Desired output:

| State    | Year | Pop   |
|----------|------|-------|
| Ohio     | 2000 | 2.5   |
| New York | 2000 | 3.6   |
| Ohio     | 2001 | None  |
| Ohio     | 2002 | None  |

I tried declaring a new dataframe outside the function with global new_census and appending the rows with new_census.append(pd.DataFrame(data)). The code I had didn't work. I tried pd.concat. That didn't work. I tried declaring a new dataframe inside the function. That didn't work.

Any help is appreciated.

2 Answers 2

3

This works for me:

def projected_pop_by_year(s):
    new_census = pd.DataFrame()
    current_pop = census[census['state'] == s]['pop'].values[0]
    current_year = census[census['state'] == s]['year'].values[0]
    i = 0; count = 1
    my_list = []
    while (i + 1) <= current_pop:

         projected_pop = None # some calculations
         data = {
             'state' : [s],
             'year' :  [current_year + count],
             'pop': [projected_pop]
         }
         my_list.append(pd.DataFrame(data))
         #print(pd.DataFrame(data))
         i += 1; count += 1
    my_list = pd.concat(my_list)
    print(census.append(pd.DataFrame(my_list)))
projected_pop_by_year("Ohio")

      state  year   pop
0      Ohio  2000   2.5
1  New York  2000   3.6
0      Ohio  2001  None
0      Ohio  2002  None

Explaination Make a list before the while Loop and save the output of the while loop by appending the list. Finally concat them together and apend with the original census dataframe.

Hope this helps.

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

Comments

1

there are several ways for adding rows to a Pandas DataFrame. When you know how to add the row, you can do it in a while/for loop in a way that matches your requirements. You can find different ways of adding a row to a Pandas DataFrame here:

https://thispointer.com/python-pandas-how-to-add-rows-in-a-dataframe-using-dataframe-append-loc-iloc/

Comments

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.