我怎么能给它一行和一列?

时间:2017-06-25 08:34:07

标签: pandas

a = df.groupby('actor_1_name')['gross'].sum()
b = df.groupby('actor_2_name')['gross'].sum()
c = df.groupby('actor_3_name')['gross'].sum()
x = pd.concat([a,b,c]).nlargest(3)
x

此代码的输出为^ enter image description here

如何让它看起来像这样:

enter image description here

Jezrael的新产品:

enter image description here

1 个答案:

答案 0 :(得分:0)

print(i,"!=", n) - DataFrame添加Series index创建新列:

x = pd.concat([a,b,c]).nlargest(3).rename_axis('name').reset_index()

样品:

s = pd.Series([3.71,3.39,3.26],index=['Johny Depp','Harrison Ford','tom Hanks'],name='gross')
print (s)
Johny Depp       3.71
Harrison Ford    3.39
tom Hanks        3.26
Name: gross, dtype: float64

df = s.rename_axis('name').reset_index()
print (df)
            name  gross
0     Johny Depp   3.71
1  Harrison Ford   3.39
2      tom Hanks   3.26
df = s.reset_index().rename(columns={'index':'name'})
print (df)
            name  gross
0     Johny Depp   3.71
1  Harrison Ford   3.39
2      tom Hanks   3.26