python - 如何根据另一个数据框中的变量从数据框中选择列
问题描述
我只想从 df2 中选择那些等于 python pandas 中 df1 变量的列
df1
parameter (column name)
a
b
c
df2
w x a c z
3 1 5 6 1
5 67 4 3 56
8 12 6 1 23
我的预期输出是
a c
5 6
4 3
6 1
解决方案
使用intersection
orisin
布尔掩码:
df3 = df2[df.columns.intersection(df1['parameter'])]
或者:
df3 = df2.loc[:, df.columns.isin(df1['parameter'])]
推荐阅读
- azure - 如何记录设备与 Azure IoT 中心的连接/断开连接
- python - 如果我的列表中没有任何内容,我该如何停止 while 循环
- android - 使用查询插入 Android 房间
- python - Python & OpenCV:尝试保存视频时出现“错误:(-215) scn == 3 || scn == 4 in function ipp_cvtColor”
- jquery - 淡入/淡出/隐藏/显示 JQuery 的问题
- java - 如何使用for循环连续图像滑块?
- sql - 为大于的值添加一个虚拟列
- youtube-data-api - Youtube API v3 使用 PlaylistItems.list 获取视频
- wix - WIX 服务安装程序
- java - 将 LinkedHashMap 转换为 Object