Let's say hypothetically I have a dataframe with one column called "Col_Strings". This column has 30k rows. I am only showing the first three rows in my table
Next I am going to add a bunch of additional columns to my dataframe.
I want to use logic that says: If my column name is contained in the row of "Col_Strings" then I want the value to say 1 ... otherwise 0.
Below is a sample table (first row is the column names):
Col_Strings 2C GAD D2 6F ABCDE
2C 1B D2 6F ABC 1 0 1 1 0
Act Dog House GAD 0 1 0 0 0
D2 6F Ant 0 0 1 1 0
Based on someone's else help I can do the following by creating a dataframe from scratch. But my question is how do I employ the above python logic when I already have a dataframe and need to reference the "Col_Strings" to determine 1 or 0?
map a function on the database column Col_Strings
, checking if the word is contained in each element. Assign the results to the column
df['Act'] = df['Col_Strings'].map(lambda x: int('Act' in x.split())
If you have a list of words to create columns for, then execute the above logic in a loop:
for word in ['Act', 'Now', 'Buy', 'MORE']:
df[word] = df['Col_Strings'].map(lambda x: int(word in x.split())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.