Python Pandas文件管理器列(按多个字符串)

2024-10-01 17:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我有一个Python 2.7 Pandas数据框架,其中有许多列,如:

['SiteName', 'SSP', 'PlatformClientCost', 'rawmachinecost', 'rawmachineprice', 'ClientBid' +... + 20 more]

我想排除所有包含“平台”或“客户端”的列,下面是我的尝试:

^{pr2}$

我在网上找不到任何相关的解决方案,所以任何帮助都会非常感谢!在

谢谢, 威尔


Tags: 数据框架客户端pandasmore平台解决方案ssp
2条回答

这是一个不错的尝试,但你的逻辑有点混乱:

col = [c for c in dataframe.columns if c.lower() not in ('platform','client') ]
print col
['SiteName', 'SSP', 'IONumber', 'userkey', 'Imps', 'PlatformClientCost', 'rawplatformcost', 'rawbidprice', 'PlatformClientBid', 'RawBidCPM', 'ClientBidCPM', 'CostCPM', 'ClientCostCPM', 'BidRatio']

仔细看看你的情况。只排除名称与“platform”和“client”完全匹配的列(无论大小写)。在

你想要的是:

^{pr2}$

如果这对你很重要的话,EdChum用熊猫的方法给出的答案可能更有效。在

使用矢量^{}

In [222]:
df = pd.DataFrame(columns=['SiteName', 'SSP', 'IONumber', 'userkey', 'Imps', 'PlatformClientCost', 'rawplatformcost', 'rawbidprice', 'PlatformClientBid', 'RawBidCPM', 'ClientBidCPM', 'CostCPM', 'ClientCostCPM', 'BidRatio'])
df.columns

Out[222]:
Index(['SiteName', 'SSP', 'IONumber', 'userkey', 'Imps', 'PlatformClientCost',
       'rawplatformcost', 'rawbidprice', 'PlatformClientBid', 'RawBidCPM',
       'ClientBidCPM', 'CostCPM', 'ClientCostCPM', 'BidRatio'],
      dtype='object')

In [223]:
df.columns[~df.columns.str.contains(r'platform|client', case=False)]
​
Out[223]:
Index(['SiteName', 'SSP', 'IONumber', 'userkey', 'Imps', 'rawbidprice',
       'RawBidCPM', 'CostCPM', 'BidRatio'],
      dtype='object')

在这里,我们可以传递一个regex模式和case=False,这样您就不需要lower,这将返回一个布尔掩码:

^{pr2}$

然后我们应用求反运算符~来反转布尔掩码并屏蔽列数组。在

相关问题 更多 >

    热门问题