Python 如何基于pandas中的上一个表创建新表?
跟随这篇文章 我想知道我是否添加了更多数据,并通过以下链接来实现这一点 结果如下所示Python 如何基于pandas中的上一个表创建新表?,python,pandas,Python,Pandas,跟随这篇文章 我想知道我是否添加了更多数据,并通过以下链接来实现这一点 结果如下所示 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 Go
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用于无排序值的新索引和列值,并通过以下方式更改位置:
谢谢你的建议。我很好地遵循了它,但我希望按照我的预期修复上一个表中的行和列result@HookIm-哦,我错过了更改过的专栏,请检查编辑过的答案。我正在考虑,我几乎要搜索如何重新列LOL。再次感谢您的建议!
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
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