PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 1, 2022

[FIXED] Why cannot I have a usual dataframe after using pivot()?

 November 01, 2022     pandas, pivot, pivot-table     No comments   

Issue

Under the variable names, there is an extra row that I do not want in my data set

fdi_autocracy = fdi_autocracy.pivot(index=["Country", "regime", "Year"], 
                                    columns="partner_regime",
                                    values =['FDI_outward', "FDI_inward", "total_fdi"],
                                   ).reset_index()
                 Country   regime  Year   FDI_outward FDI_inward total_fdi
partner_regime                               0.0        0.0       0.0
0                Albania    0.0  1995         NaN        NaN       NaN
1                Albania    0.0  1996         NaN        NaN       NaN
2                Albania    0.0  1997         NaN        NaN       NaN
3                Albania    0.0  1998         NaN        NaN       NaN
4                Albania    0.0  1999         NaN        NaN       NaN

What I want is following:

                 Country   regime  Year   FDI_outward FDI_inward total_fdi
                                       
0                Albania    0.0  1995         NaN        NaN       NaN
1                Albania    0.0  1996         NaN        NaN       NaN
2                Albania    0.0  1997         NaN        NaN       NaN
3                Albania    0.0  1998         NaN        NaN       NaN
4                Albania    0.0  1999         NaN        NaN       NaN

Solution

IIUC, you don't need the partner_regime? this removes that title

fdi_autocracy.rename_axis(columns=[None, None])


Answered By - Naveed
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing