如何基于熊猫中的上一个表创建新表?

时间:2019-04-02 10:49:44

标签: python pandas

此帖子之后 How to reorder indexed rows based on a list in Pandas data frame

import pandas as pd
df = pd.DataFrame({'name' : ['A', 'Z','C'],
                   'company' : ['Apple', 'Yahoo','Amazon'],
                   'height' : [130, 150,173]})

df = df.pivot(index="name", columns="company", values="height").fillna(0)

df.reindex(["Z", "C", "A"])


company Amazon  Apple   Yahoo
name            
   Z     0.0    0.0     150.0
   C.  173.0    0.0      0.0
   A     0.0   130.0     0.0

我想知道是否添加了更多数据,并通过关注此链接Is there a way to copy only the structure (not the data) of a Pandas DataFrame?

df_1 = pd.DataFrame({'name' : ['A','Z','B','C','D'],
                   'company' : ['Apple','Yahoo','Alebaba','Amazon','Google'],
                   'height' : [130, 150,160,173,180]})

df_1 = df_1.pivot(index="name", columns="company", values="height").fillna(0)

df_1 = df_1.reindex_like(df)

结果如下所示

company Amazon  Apple   Yahoo
    name            
       Z     0.0    0.0     150.0
       C   173.0    0.0      0.0
       A     0.0   130.0     0.0

但是我希望看到这样的结果

company Amazon  Apple   Yahoo   Alebaba Google
name                    
 Z       0.0    0.0     150.0    0.0    0.0
 C     173.0    0.0       0.0    0.0    0.0
 A       0.0    130.0     0.0    0.0    0.0
 B       0.0    0.0       0.0   160.0   0.0
 D       0.0    0.0       0.0    0.0    180.0

使用少量数据就可以了,但是如果有数千个数据,我该如何解决此问题?

要添加到先前数据中的数据集可以位于任何位置。

有什么建议吗? T T

1 个答案:

答案 0 :(得分:2)

Index.differenceIndex.append一起用于没有排序值的新索引和列值,并通过DataFrame.reindex更改位置:

print (df_1.index.difference(df.index))
Index(['B', 'D'], dtype='object', name='name')

print (df.index.append(df_1.index.difference(df.index)))
Index(['Z', 'C', 'A', 'B', 'D'], dtype='object', name='name')

idx = df.index.append(df_1.index.difference(df.index))
cols = df.columns.append(df_1.columns.difference(df.columns))
df_1 = df_1.reindex(index=idx, columns=cols)
print (df_1)
company  Amazon  Apple  Yahoo  Alebaba  Google
name                                          
Z           0.0    0.0  150.0      0.0     0.0
C         173.0    0.0    0.0      0.0     0.0
A           0.0  130.0    0.0      0.0     0.0
B           0.0    0.0    0.0    160.0     0.0
D           0.0    0.0    0.0      0.0   180.0
相关问题