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

pandasでカテゴリごとのone-hotエンコーディング

More than 1 year has passed since last update.

Abstract

pandasでカテゴリごとに最大値、最小値を抽出するのは、難しくないです。
今回は、最大値、最小値にラベルをつけるスクリプト考えていて、結構長めにつまったのでメモを残します。

Situation

idx category_id id incremental
0 1 1 1
1 1 2 2
2 1 3 3
3 2 4 1
4 2 5 2
5 2 6 3
... ... ... ...

みたいなテーブルを考えます。
この時、以下の問いを考えます。

カテゴリごとの最大値、最小値に対してフラグを持たせる

最終的には、こんな感じに慣ればいいです。

idx category_id id incremental is_first is_last
0 1 1 1 1 0
1 1 2 2 0 0
2 1 3 3 0 1
3 2 4 1 1 0
4 2 5 2 0 0
5 2 6 3 0 1
... ... ... ... ... ...

Script

csvを読むとして書きます。

import pandas as pd 

df = pd.read_csv('file_name')

df['is_first'] = np.where(df.index.isin(df.groupby('category_id').incremental.idxmin()), 1, 0)
df['is_last']  = np.where(df.index.isin(df.groupby('category_id').incremental.idxmax()), 1, 0)

カテゴリが増えてもとりあえず、対応はできます。

TODO

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

が出ます。現状、解決策見つかっていないので改善の余地ありです。

Why do not you register as a user and use Qiita more conveniently?
  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
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