df12.head()
            COMPONENT_ID   PSRO       binned   PSRO_SPEED
4080  11S02CY383YH1934794910  7.470  (7.4, 7.65]  (7.4, 7.65]
4722  11S02CY388YH1934786330  7.491  (7.4, 7.65]  (7.4, 7.65]
4708  11S02CY388YH1934782718  7.497  (7.4, 7.65]  (7.4, 7.65]
4726  11S02CY388YH1934786336  7.564  (7.4, 7.65]  (7.4, 7.65]
4707  11S02CY388YH1934782709  7.581  (7.4, 7.65]  (7.4, 7.65]

Saya ingin data binned itu dipetakan ke nilai yang berbeda. Saya mencoba

df12['PSRO_SPEED']=df12['PSRO_SPEED'].map({'(7.4,7.65]': 'high_speed'})

Tapi ini tidak bekerja. Itu mengubah df12['PSRO_SPEED'] menjadi NAN.

0
Chinti 23 Maret 2020, 14:49

1 menjawab

Jawaban Terbaik

Saya pikir ini Interval, bukan string, jadi solusi yang mungkin adalah:

i = pd.Interval(7.4,7.65, closed='right')
df12['PSRO_SPEED']=df12['PSRO_SPEED'].map({i: 'high_speed'})

Atau untuk solusi Anda diperlukan konversi kolom ke string:

df12['PSRO_SPEED']=df12['PSRO_SPEED'].astype(str).map({'(7.4,7.65]': 'high_speed'})

Tapi sebaiknya tambahkan parameter label ke cut:

bins = [7.4,7.65,7.9,8.15,8.4,8.65] 
labels = ['lowest','low','medium','great','greatest']
df12['binned'] = pd.cut(df12['PSRO'], bins=bins, labels=labels)
1
jezrael 23 Maret 2020, 12:14