私はパンダのデータフレームを、次います変更値value_countsに応じて()
import pandas as pd
from pandas import Series, DataFrame
data = DataFrame({'Qu1': ['apple', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'egg'],
'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
'Qu3': ['apple', 'potato', 'sausage', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'egg']})
値が大きいカウントまたは一部に等しいとき、私はvalue_counts()
によるQu3
、列Qu1
、Qu2
内の値を変更したいですQu1
カラム
>>> pd.value_counts(data.Qu1) >= 2
cheese True
potato True
banana True
apple False
egg False
ため例えば数
それぞれの値に少なくとも2つの出現があるので、値cheese
、potato
、banana
を保持したいと思います。
値apple
とegg
から、私は何も変更列Qu2
について値others
を作成していないしたいと思います:
>>> pd.value_counts(data.Qu2) >= 2
banana True
apple True
sausage True
test_data
test_data = DataFrame({'Qu1': ['other', 'potato', 'cheese', 'banana', 'cheese', 'banana', 'cheese', 'potato', 'other'],
'Qu2': ['sausage', 'banana', 'apple', 'apple', 'apple', 'sausage', 'banana', 'banana', 'banana'],
'Qu3': ['other', 'potato', 'other', 'cheese', 'cheese', 'potato', 'cheese', 'potato', 'other']})
添付のおかげで、最終結果!
かなり多くエレガント&私のアプローチよりも早く(.replace 'と)'! – Stefan
@StefanJansenありがとうございました。 :)私の経験では、 '.replace()'は一般的に '.map()'よりも遅いので、両方が可能な場合にマップを使う傾向があります。私はまだapply-map-value_countsの組み合わせが繰り返すかもしれないと思っていますが、より良い選択肢を見つけることができませんでした。 – ayhan
ありがとう!エレガントなソリューション。 '.where()> = 2'の仕組みはどうですか? – Toren