<p>我有一个大熊猫数据框(>;100列)。我需要删除不同的列,我希望有一种使用旧列的方法</p>
<pre><code>df.drop(df.columns['slices'],axis=1)
</code></pre>
<p>我做了如下选择:</p>
<pre><code>a = df.columns[3:23]
b = df.colums[-6:]
</code></pre>
<p>因为<code>a</code>和<code>b</code>表示要删除的列集。</p>
<p>以下</p>
<pre><code>list(df)[3:23]+list(df)[-6:]
</code></pre>
<p>生成正确的选择,但我不能用<code>drop</code>实现它:</p>
<pre><code>df.drop(df.columns[list(df)[3:23]+list(df)[-6:]],axis=1)
</code></pre>
<blockquote>
<p>ValueError: operands could not be broadcast together with shapes (20,)
(6,)</p>
</blockquote>
<p>我环顾四周,却找不到答案。</p>
<p><a href="https://stackoverflow.com/questions/33042633/selecting-last-n-columns-and-excluding-last-n-columns-in-dataframe">Selecting last n columns and excluding last n columns in dataframe</a></p>
<p>(以下与我收到的错误有关):</p>
<p><a href="https://stackoverflow.com/questions/24560298/python-numpy-valueerror-operands-could-not-be-broadcast-together-with-shapes">python numpy ValueError: operands could not be broadcast together with shapes</a></p>
<p>这一个让人觉得他们也有类似的问题,但“切片”并不是分开的:
<a href="https://stackoverflow.com/questions/28538536/deleting-multiple-columns-based-on-column-names-in-pandas">Deleting multiple columns based on column names in Pandas</a></p>
<p>干杯</p>