在Python Pandas中删除数据框行名

问题描述

我在Pandas中有一个这样的数据框:

# https://www.datasciencemadesimple.com/reshape-long-wide-pandas-python-pivot-function/
import pandas as pd
import numpy as np 

from platform import python_version
print(python_version()) #3.7.7

#Create a DataFrame

d = {'countries':['A','B','C','A','C'],'continent': ['Europe','Asia','Africa','Europe','Africa'],'metrics':['population_in_million','population_in_million','gdp_percapita','gdp_percapita'],'values':[100,200,120,2000,7000,15000] }

df = pd.DataFrame(d,columns=['countries','continent','metrics','values'])
df

然后我使用以下代码将其转换为宽数据框:

# https://stackoverflow.com/questions/47178861/pandas-long-to-wide-format-with-multi-index
test_df = pd.DataFrame(df.pivot_table(index=['countries','continent'],columns='metrics',values='values'))

test_df = test_df.reset_index() 
test_df

enter image description here

我正在寻找一种删除“指标”的方法。先前的答案没有帮助。我的Python版本是3.7.7

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)