python - 如何根据 pandas 中的前一个表创建新表?

标签 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

这对于小数据来说没问题,但是如果有数千个数据,我该如何解决这个问题?

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

有什么建议吗? TT

最佳答案

使用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

关于python - 如何根据 pandas 中的前一个表创建新表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55472943/

相关文章:

python - 仅当没有给定前缀和任意数量的空格时才匹配单词

python - 在有序计数器中打印第一个键值

pandas - 使用 Pandas 将 csv 转换为 Parquet 文件时出错

python - 使用 Lettuce 的 BDD 测试能否替代项目中的所有其他形式的测试?

python - 使用 Python 拆分 ps 的输出

python - 从两个数据帧计算一个新的 pandas DataFrame

python - 如何优化多列的条件累积和

python - 数据框中几个属性的数据描述

python - 使用值列表填充空单元格

python - 如何使用新的索引和列更新 Pandas DataFrame