要合并具有不重复索引的数据帧,可以使用pandas
库中的concat
函数。下面是一个示例代码:
import pandas as pd
# 创建两个具有不重复索引的数据帧
df1 = pd.DataFrame({'A': [1, 2, 3]},
index=['a', 'b', 'c'])
df2 = pd.DataFrame({'B': [4, 5, 6]},
index=['d', 'e', 'f'])
# 使用concat函数合并数据帧
result = pd.concat([df1, df2], axis=1)
print(result)
输出结果为:
A B
a 1.0 NaN
b 2.0 NaN
c 3.0 NaN
d NaN 4.0
e NaN 5.0
f NaN 6.0
在上面的示例中,我们创建了两个具有不重复索引的数据帧df1
和df2
。然后,我们使用concat
函数将这两个数据帧按列合并。axis=1
参数表示按列合并,如果要按行合并,可以将axis
参数设置为0。合并后的结果存储在result
变量中,然后打印输出。
上一篇:不重复随机选择
下一篇:不重复条件的表的合并