[英]Simple join of two Data Frames in Pandas
I have two data frames in a Python program using Pandas. 我在使用Pandas的Python程序中有两个数据框。 I am new to Pandas.
我是熊猫新手。
Each one has a number of columns and rows - the first is similar to: 每个都有许多列和行-第一个类似于:
calc_1 calc_2 calc_3
0 34.3 43.1 42.0
2 3.0 4.0 5.0
3 6.1 6.1 6.2
4 4.2 4.3 4.5
The second is similar to: 第二个类似于:
gender age
0 M 25
2 M 27
3 M 27
4 F 36
For every integer row index there is a corresponding entry in the second data frame. 对于每个整数行索引,第二个数据帧中都有一个对应的条目。 I would like to join them into a resulting data frame where the row indices are equal, such as an inner join in SQL.
我想将它们连接到行索引相等的结果数据框中,例如SQL中的内部连接。
I can't seem to get this correct. 我似乎无法正确理解。 Append results in 2 x the number of rows I should have.
追加结果是我应具有的行数的2倍。 The info is coming in from a CSV.
该信息来自CSV。
calc_1 calc_2 calc_3 gender age
0 34.3 43.1 42.0 M 25
2 3.0 4.0 5.0 M 27
3 6.1 6.1 6.2 M 27
4 4.2 4.3 4.5 F 36
I would like to preserve the column order if possible when joining. 我想在加入时保留列顺序。
EDIT: 编辑:
I can't seem to use merge because the integer index has no name 我似乎无法使用合并,因为整数索引没有名称
pd.merge(df1, df2, on='?????', how='inner')
Use pd.concat
and pass axis=1
to concatenate column-wise: 使用
pd.concat
并传递axis=1
来按列连接:
In [37]:
pd.concat([df,df1], axis=1)
Out[37]:
calc_1 calc_2 calc_3 gender age
0 34.3 43.1 42.0 M 25
2 3.0 4.0 5.0 M 27
3 6.1 6.1 6.2 M 27
4 4.2 4.3 4.5 F 36
In [38]:
df.join(df1)
Out[38]:
calc_1 calc_2 calc_3 gender age
0 34.3 43.1 42.0 M 25
2 3.0 4.0 5.0 M 27
3 6.1 6.1 6.2 M 27
4 4.2 4.3 4.5 F 36
Or merge
and set left_index=True
and right_index=True
: 或
merge
并设置left_index=True
和right_index=True
:
In [41]:
df.merge(df1, left_index=True, right_index=True)
Out[41]:
calc_1 calc_2 calc_3 gender age
0 34.3 43.1 42.0 M 25
2 3.0 4.0 5.0 M 27
3 6.1 6.1 6.2 M 27
4 4.2 4.3 4.5 F 36
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.