2

In pandas have a dataframe:

df1 = pd.DataFrame({'Type':['Application','Application','Hardware'],
                    'Category': ['None','None','Hardware']})

I have the following index to retrieve rows where type contains "application" and Category contains 'None'.

df1[df1['Type'].str.contains('Application') & df1['Category'].str.contains('None')]

Category    Type
0   None    Application
1   None    Application

I would like to update the column Category such that the value is 'some new value' for each row.

I have also tried the same with the following loc index with no success

df1[df1.loc[:,'Type'].str.contains('Application') \
  & df1.loc[:,'Category'].str.contains('None')]
1
  • Please include an example of the expected output. Also, confirm that "None" is a string, not the Python None. Commented Aug 17, 2017 at 22:52

1 Answer 1

1

Are you looking for this?

df1.loc[(df1['Type'] == 'Application') & (df1['Category'] == 'None'), 'Category'] = 'New category'


    Category        Type
0   New category    Application
1   New category    Application
2   Hardware        Hardware
Sign up to request clarification or add additional context in comments.

1 Comment

kindly using df.loc[(df['Type'].str.contains('Application')) & (df['Category'].str.contains('None')), 'Category'] = 'New category' maybe the column not exactly same. ~

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.