我在pandas中有一个名为df_A
的数据帧,它实时地有100多列。你知道吗
另外,我还有另一个数据帧df_B
,其中两列给出了我需要从df_A
中得到哪些列
下面给出了一个可复制的例子
import pandas as pd
d = {'foo':[100, 111, 222],
'bar':[333, 444, 555],'foo2':[110, 101, 222],
'bar2':[333, 444, 555],'foo3':[100, 111, 222],
'bar3':[333, 444, 555]}
df_A = pd.DataFrame(d)
d = {'ReqCol_A':['foo','foo2'],
'bar':[333, 444],'foo2':[100, 111],
'bar2':[333, 444],'ReqCol_B':['bar3', ''],
'bar3':[333, 444]}
df_b = pd.DataFrame(d)
在上面的例子中可以看到,df_b
,ReqCol_A
和ReqCol_B
下的值是我试图从df_A
得到的
因此,我的预期输出将有来自df_A
的三列。这三列分别是foo2和bar3。你知道吗
df_C
将是预期的输出
df_C
foo foo2 bar3
100 110 333
111 101 444
222 222 555
请帮帮我。我正在努力得到这个。你知道吗
尝试使用
filter
仅获取那些带有'ReqCol'的列,然后stack
获取列表并过滤db\u数据帧:输出:
解决方案:
性能比较
给定方法:
第二个答案(使用过滤器):
显然,给定的方法比其他方法快得多。你知道吗
相关问题 更多 >
编程相关推荐