Help us understand the problem. What is going on with this article?

Dataframeの不要な列を取り除く方法

More than 1 year has passed since last update.

Dataframeから、columnが特定の文字列で始まる列をまとめて取り除く方法

少しだけ列を簡単に取り除く

dropを使えばわりと簡単ですが、書き下す必要があって面倒です。

df.drop(columns=[column1, column2, ...])

複数の列を同時に取り除く

大量のcolumnsを持つdataframeから、特定の文字列、例えば'filter'で始まるcolmunsの列データだけ取り除いたdataframeを取得したかったので、以下のようにしました。

df.loc[:, [not s.startswith('filter') for s in df.columns.tolist()]]

columnsをリストにしてから1つ1つcolumn名を取りだして、指定の文字列(ここでは'filter')で始まるかを判定した上で、条件に当てはまるところを削除したいので、notをつけて真偽判定を逆にしています。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした