1

So, I am quite new to python, and this my first ever stack overflow post.

So I have a column in data frame that contains actions in poker hand abbreviated as letters.

pdb_RED.PRFLOP_A.unique()
array(['BrA', 'Brf', 'Bk', 'f', 'Bc', 'r', 'Bf', 'c', 'B', 'Br', 'cc',
   'Bcf', 'Bcc', 'BcrA', 'Brc', 'BQ', 'Brr', 'rf', 'rc', 'BrQ', 'BcA',
   'cr', 'cf', 'Q', 'fQ', 'BKQ', 'rr', 'BrcA', 'rrc', 'Bcr', 'BcQ',
   'BA', 'rQ', 'BfQ', 'rA', 'KQ', 'rrA'], dtype=object)

I want to create a new column in my data frame, that contains dummy variable for 1, if the expression is matched in a column, and 0 for everything else. Expression:

A1_agro=pdb_RED['PRFLOP_A'].str.contains('[bBrA]$', regex=True)

It returns a tuple with True/False values. (So 1 for True , 0 for False i need) I proceed by creating an empty column in my original data frame, and later follow with an attempt with my limited knowledge. I hope someone can give my directions, because it returns 'tuple' object does not support item assignment' error.

pdb_RED['PRFLOP_agro']=np.nan
for row in pdb_RED.iterrows():
    if A1_agro[1]==True:
        row['PRFLOP_agro']=1
    else:
        row['PRFLOP_agro']=0

1 Answer 1

1

I think your code will work if you change your loop to

for i, row in pdb_RED.iterrows():

However there really should be no need to iterate through the rows, you can assign your new column directly to your original dataframe:

pdb_RED['A1_agro'] = pdb_RED['PRFLOP_A'].str.contains('[bBrA]$', regex=True)

If you want integers rather than boolean, you can add .astype(int) at the end.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, single line solution does the job perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.