python - 在某些列上交错 2 个数据帧
问题描述
我有 2 个数据框
df1:
StartLocation,StartDevice,StartPort,EndLocation,EndDevice,EndPort,LinkType,Speed
DD1,Switch1,P1,AD1,Switch2,P2,MTP,1000
DD2,Switch2,P3,AD2,Switch3,P2,MTP,1000
DD3,Switch3,P5,AD3,Switch4,P6,MTP,1000
df2:
StartLocation,StartDevice,StartPort,EndLocation,EndDevice,EndPort
AB11,RU15,P1,AJ11,RU25,P2
AB12,RU18,P2,AB11,RU35,P2
AB13,RU19,P3,AB11,RU40,P4
我想交错两个数据帧,我尝试了一些选项,但似乎无法让它工作。我使用以下代码接近功能,但它没有加入适当的列
import pandas as pd
from toolz import interleave
df3 = pd.DataFrame(interleave([df1.values, df2.values]), columns=df1)
预期输出看起来像
StartLocation,StartDevice,StartPort,EndLocation,EndDevice,EndPort,LinkType,Speed
DD1,Switch1,P1,AD1,Switch2,P2,MTP,1000
AB11,RU15,P1,AJ11,RU25,P2,nan,nan
DD2,Switch2,P3,AD2,Switch3,P2,MTP,1000
AB12,RU18,P2,AB11,RU35,P2,nan,nan
DD3,Switch3,P5,AD3,Switch4,P6,MTP,1000
AB13,RU19,P3,AB11,RU40,P4,nan,nan
我认为它应该很简单,但我找不到合适的语法。任何人都可以给出任何想法吗?
在此先感谢您的帮助!
解决方案
如果列名相同,唯一的区别是可以使用 DataFrame 之一中的一些新列名:
df3 = pd.DataFrame(interleave([df1.values, df2.values]), columns=df1.columns)
print (df3)
StartLocation StartDevice StartPort EndLocation EndDevice EndPort LinkType \
0 DD1 Switch1 P1 AD1 Switch2 P2 MTP
1 AB11 RU15 P1 AJ11 RU25 P2 None
2 DD2 Switch2 P3 AD2 Switch3 P2 MTP
3 AB12 RU18 P2 AB11 RU35 P2 None
4 DD3 Switch3 P5 AD3 Switch4 P6 MTP
5 AB13 RU19 P3 AB11 RU40 P4 None
Speed
0 1000.0
1 NaN
2 1000.0
3 NaN
4 1000.0
5 NaN
之前使用了适用于任何列名称的更通用解决方案,DataFrame.align
以防止每个 DataFrame 正确对齐列:
print (df1)
EndDevice EndLocation EndPort LinkType Speed StartDevice StartLocation \
0 Switch2 AD1 P2 MTP 1000 Switch1 DD1
1 Switch3 AD2 P2 MTP 1000 Switch2 DD2
2 Switch4 AD3 P6 MTP 1000 Switch3 DD3
StartPort
0 P1
1 P3
2 P5
print (df2)
EndDevice EndLocation EndPort LinkType Speed StartDevice StartLocation \
0 RU25 AJ11 P2 NaN NaN RU15 AB11
1 RU35 AB11 P2 NaN NaN RU18 AB12
2 RU40 AB11 P4 NaN NaN RU19 AB13
StartPort
0 P1
1 P2
2 P3
df3 = pd.DataFrame(interleave([df1.values, df2.values]), columns=df1.columns)
print (df3)
EndDevice EndLocation EndPort LinkType Speed StartDevice StartLocation \
0 Switch2 AD1 P2 MTP 1000.0 Switch1 DD1
1 RU25 AJ11 P2 NaN NaN RU15 AB11
2 Switch3 AD2 P2 MTP 1000.0 Switch2 DD2
3 RU35 AB11 P2 NaN NaN RU18 AB12
4 Switch4 AD3 P6 MTP 1000.0 Switch3 DD3
5 RU40 AB11 P4 NaN NaN RU19 AB13
StartPort
0 P1
1 P1
2 P3
3 P2
4 P5
5 P3
另一个想法Index.union
and DataFrame.reindex
:
cols = df1.columns.union(df2.columns, sort=False)
df1 = df1.reindex(cols, axis=1)
df2 = df2.reindex(cols, axis=1)
print (df1)
StartLocation StartDevice StartPort EndLocation EndDevice EndPort LinkType \
0 DD1 Switch1 P1 AD1 Switch2 P2 MTP
1 DD2 Switch2 P3 AD2 Switch3 P2 MTP
2 DD3 Switch3 P5 AD3 Switch4 P6 MTP
Speed
0 1000
1 1000
2 1000
print (df2)
StartLocation StartDevice StartPort EndLocation EndDevice EndPort LinkType \
0 AB11 RU15 P1 AJ11 RU25 P2 NaN
1 AB12 RU18 P2 AB11 RU35 P2 NaN
2 AB13 RU19 P3 AB11 RU40 P4 NaN
Speed
0 NaN
1 NaN
2 NaN
df3 = pd.DataFrame(interleave([df1.values, df2.values]), columns=cols)
print (df3)
StartLocation StartDevice StartPort EndLocation EndDevice EndPort LinkType \
0 DD1 Switch1 P1 AD1 Switch2 P2 MTP
1 AB11 RU15 P1 AJ11 RU25 P2 NaN
2 DD2 Switch2 P3 AD2 Switch3 P2 MTP
3 AB12 RU18 P2 AB11 RU35 P2 NaN
4 DD3 Switch3 P5 AD3 Switch4 P6 MTP
5 AB13 RU19 P3 AB11 RU40 P4 NaN
Speed
0 1000.0
1 NaN
2 1000.0
3 NaN
4 1000.0
5 NaN
推荐阅读
- azure - 从 Azure DevOps 获取组织/实例名称列表
- android - 如何为具有各种片段的 Kotlin Android 应用程序创建带有协程的单例计时器?
- shopify - 如何在 shopify 中创建自定义模块(CRUD)?喜欢,产品和导航
- sql - 多行的 SQL 查询
- node.js - npm install bcrypt@3.0.6 使用节点 6.12.1 失败
- sql-server - 使用一个表中的 id 连接 4 个表并为 2 个表求和数量
- php - 如何通过 jQuery AJAX 将 php 变量拉入 js 对象的属性中?
- kubernetes - 如何获取部署在 Kubernetes 中的应用程序的延迟?
- algorithm - 如何在有效的时间内解决 Kakurasu Puzzle?
- ios - 在 Swift 中创建常量文件的最佳方法是什么?请检查说明