Pandas 合并两个行数相同的数据集 [英] Pandas merge two datasets with same number of rows
问题描述
我有两个行数相同的表(第二个表是通过处理 T1 中的文本从第一个表计算出来的).我将它们都存储为熊猫数据框.T2 与 T1 没有共同的列.这是示例,因为我的表很大:
I have two tables with same number of rows (second table is computed from first one by processing of text inside T1). I have both of them stored as pandas dataframe. T2 is no common column with T1. This is example because my tables are huge:
T1:
| name | street | city |
|-------|---------|--------|
| David | street1 | Prague |
| John | street2 | Berlin |
| Joe | street3 | London |
T2:
| computed1 | computed2 |
|-----------|-----------|
| 0.5 | 0.3 |
| 0.2 | 0.8 |
| 0.1 | 0.6 |
Merged:
| name | street | city | computed1 | computed2 |
|-------|---------|--------|-----------|-----------|
| David | street1 | Prague | 0.5 | 0.3 |
| John | street2 | Berlin | 0.2 | 0.8 |
| Joe | street3 | London | 0.1 | 0.6 |
我尝试了这些命令:
pd.concat([T1,T2])
pd.merge([T1,T2])
result=T1.join(T1)
使用 concat 和 merge 我只会得到前一千个组合,其余的填充为 nan(我仔细检查了两者的大小相同),并且使用 .join 不会组合它们,因为没有任何共同点.
With concat and merge I will get only first thousand combined and rest is filled with nan (I double checked that both are same size), and with .join it not combine them because there is nothing in common.
有什么办法可以在pandas中组合这两个表吗?
Is there any way how to combine these two tables in pandas?
谢谢
推荐答案
您需要 reset_index()
在 concat
之前获得默认索引:
You need reset_index()
before concat
for default indices:
df = pd.concat([T1.reset_index(drop=True),T2.reset_index(drop=True)], axis=1)
这篇关于Pandas 合并两个行数相同的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!