因此,我正在使用Python尝试更改数据帧的索引。 这是我的密码:
df = pd.read_csv("data_file.csv", na_values=' ')
table = df['HINCP'].groupby(df['HHT'])
print(table.describe()[['mean', 'std', 'count', 'min', 'max']].sort_values('mean', ascending=False))
以下是当前的数据帧:
mean std count min max
HHT
1.0 106790.565562 100888.917804 25495.0 -5100.0 1425000.0
5.0 79659.567376 74734.380152 1410.0 0.0 625000.0
7.0 69055.725901 63871.751863 1193.0 0.0 645000.0
2.0 64023.122122 59398.970193 1998.0 0.0 610000.0
3.0 49638.428821 48004.399101 5718.0 -5100.0 609000.0
4.0 48545.356298 60659.516163 5835.0 -5100.0 681000.0
6.0 37282.245015 44385.091076 8024.0 -11200.0 676000.0
我希望索引值是这样的,而不是编号为1,2,…,7:
Married couple household
Nonfamily household:Male
Nonfamily household:Female
Other family household:Male
Other family household:Female
Nonfamily household:Male
Nonfamily household:Female
我尝试使用set_index()
作为table
的属性,其中我将键设置为我想要的上面的索引列表,但这会导致以下错误:
AttributeError: 'SeriesGroupBy' object has no attribute 'set_index'
我还想知道是否有任何方法可以改变索引顶部的HHT标签,或者改变索引值会带来什么
使用dict映射到
map
带有HHT标签的HHT数值将更加健壮:编辑:在分组之前,请在
pums_df
上尝试映射使用
map
创建一个新的label
列:使用新的
label
到groupby
:相关问题 更多 >
编程相关推荐