Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python how to convert each word of each row to numeric value of a dataframe

You can use explode to get words then map with your dict and reshape your dataframe:

MAPPING = {'I': 30,'am': 45,'good': 90,'boy': 50,'We':100,'are':70,'going':110}

df['documents'] = (df['documents'].str.split().explode().map(MAPPING).astype(str)
                                  .groupby(level=0).agg(list).str.join(' '))
print(df)

# Output
   id    documents
0   0  30 45 90 50
1   1   100 70 110
2   2    30 45 110
Step by step

Phase 1: Explode

# Split phrase into words
>>> out = df['documents'].str.split()
0    [I, am, good, boy]
1      [We, are, going]
2        [I, am, going]
Name: documents, dtype: object

# Explode lists into scalar values
>>> out = out.explode()
0        I
0       am
0     good
0      boy
1       We
1      are
1    going
2        I
2       am
2    going
Name: documents, dtype: object
Phase 2: Transform

# Convert words with your dict mapping and convert as string
>>> out = out.map(MAPPING).astype(str)
0     30
0     45
0     90
0     50
1    100
1     70
1    110
2     30
2     45
2    110
Name: documents, dtype: object  # <- .astype(str)
Phase 3: Reshape

# Group by index (level=0) then aggregate to a list
>>> out = out.groupby(level=0).agg(list)
0    [30, 45, 90, 50]
1      [100, 70, 110]
2       [30, 45, 110]
Name: documents, dtype: object

# Join your list of words
>>> out = out.str.join(' ')
0    30 45 90 50
1     100 70 110
2      30 45 110
Name: documents, dtype: object
Comment

PREVIOUS NEXT
Code Example
Python :: List change after copy Python 
Python :: python regex exclude letters 
Python :: tensorflow 1.x spp implementation 
Python :: vscode show when variable is protected or private python 
Python :: get distance between points in 1 array pythoin 
Python :: deque popleft in python 
Python :: make python present number in sciencetifc 
Python :: long armstrong numbers 
Python :: how do i add two matrix and store it in a list in python 
Python :: string exercise 
Python :: Redirect to the same page and display a message if user insert wrong data 
Python :: Determining the Data Type 
Python :: get command line variables python 
Python :: check if id is present in elasticsearch using python 
Python :: python discord next page 
Python :: ring add new items to the list using the string index 
Python :: negative max in python 
Python :: python data statics 
Python :: install open3d jetson nano aarch64 
Python :: Start of my python career 
Python :: python message from byte 
Python :: python mayusculas 
Python :: rounding with .2g gives strange results 
Python :: python making player inventory 
Python :: scrollable dataframe 
Python :: nunique sort 
Python :: disable json dumps encode 
Python :: python gpsd client 
Python :: how to add import pydictionary in python 
Python :: django create superuser with first_name 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =