Python Forum
[pandas] Convert categorical data to numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pandas] Convert categorical data to numbers
#1
Hello,

I have a data frame df_train which has a column sub_division.

The values in the column is look like below

ABC_commercial
ABC_Private
Test ROM DIV
ROM DIV
TEST SEC ROM

I am trying to
1. convert anything starts with ABC* to a number (for ex: 1)
2. convert anything contains ROM to a number (for ex: 2)

Can you suggest please?

Thanks in advance.
Reply
#2
A possibility that might be useful for you:
import pandas as pd

s = pd.Series(['ABC_commercial', 'ABC_Private', 'Test ROM DIV', 'ROM DIV', 'TEST SEC ROM'], dtype="object")
df = pd.DataFrame(s, columns=['sub_division'])

df['ABC'] = (df.sub_division.str.find('ABC_') > -1) * 1
df['ROM'] = (df.sub_division.str.find('ROM') > -1) * 1

print(df)
Output:
     sub_division  ABC  ROM
0  ABC_commercial    1    0
1     ABC_Private    1    0
2    Test ROM DIV    0    1
3         ROM DIV    0    1
4    TEST SEC ROM    0    1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping in pandas/multi-index data frame Aleqsie 3 606 Jan-06-2024, 03:55 PM
Last Post: deanhystad
Smile How to further boost the data read write speed using pandas tjk9501 1 1,228 Nov-14-2022, 01:46 PM
Last Post: jefsummers
Thumbs Up can't access data from URL in pandas/jupyter notebook aaanoushka 1 1,830 Feb-13-2022, 01:19 PM
Last Post: jefsummers
Question Sorting data with pandas TheZaind 4 2,295 Nov-22-2021, 07:33 PM
Last Post: aserian
  Pandas Data frame column condition check based on length of the value aditi06 1 2,655 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  [Pandas] Write data to Excel with dot decimals manonB 1 5,774 May-05-2021, 05:28 PM
Last Post: ibreeden
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 2,418 Feb-20-2021, 08:25 PM
Last Post: perfringo
  pandas read_csv can't handle missing data mrdominikku 0 2,459 Jul-09-2020, 12:26 PM
Last Post: mrdominikku
  Pandas data frame creation from Kafka Topic vboppa 0 1,913 Jul-01-2020, 04:23 PM
Last Post: vboppa
  Generate Test data (.csv) using Pandas Ashley 5 3,005 Jun-15-2020, 02:51 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020