Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 3812
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T03:07:07+00:00 2022-06-03T03:07:07+00:00

python – Adding observations for each id with missing year observations in unbalanced panel

  • 0

[ad_1]

Suppose we are working with an unbalanced panel that looks like the below dataframe.

df = pd.DataFrame({'id': ['1', '1', '1', '2', '2', '3', '4', '4'], 'Year': [2000, 2001, 2003, 2004, 2005, 2002, 2001, 2003], 'Var': [1, 4, 6, 8, 10, 12, 15, 17]})

print(df)
  id  Year  Var
0  1  2000    1
1  1  2001    4
2  1  2003    6
3  2  2004    8
4  2  2005   10
5  3  2002   12
6  4  2001   15
7  4  2003   17

The objective is to add rows with variable column equal to zero for ids that do not contain consecutive observations. For example, since id =1 has a row reported for 2000, 2001, and 2003, we need to aded the following row to the dataframe

df_append = pd.DataFrame({'id':[1], 'Year':[2002], 'Var':[0]})

We would also need to do this for id 4.

df_append2 = pd.DataFrame({'id':[4], 'Year':[2002], 'Var':[0]})

We would then retrieve the following dataframe:

  id  Year  Var
0  1  2000    1
1  1  2001    4
2  1  2002    0
3  1  2003    6
4  2  2004    8
5  2  2005   10
6  3  2002   12
7  4  2001   15
8  4  2002   0
9  4  2003   17

Was thinking of doing this in a forloop but did not know how to retrieve a dictionary of id and year made up of those that are missing observations.

[ad_2]

  • 0 0 Answers
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8057 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1842 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1925 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.