Pandas loc multiple conditions [duplicate] - pandas

This question already has answers here:
Selecting with complex criteria from pandas.DataFrame
(5 answers)
Closed 3 years ago.
I have a dataframe and I want to delete all rows where column A is equal to blue and also col B is equal to green.
I though the below should work, but its not the case.
Can anyone see the problem
df=df.loc[~(df['A']=='blue' & df['B']=='green')]

You should separate the two propositions:
df1=df.loc[~(df['A']=='blue') & ~(df['B']=='green')]

use eq instead of ==:
df.loc[~(df['A'].eq('blue') & df['B'].eq('green'))]

query
Note the != and or as consequence of De Morgan's Law
df.query('A != "blue" or B != "green"')

Related

Hey can anybody tell me what is the meaning of this? [duplicate]

This question already has answers here:
What is [:,:-1] in python? [duplicate]
(2 answers)
Python Difference between iloc indexes
(1 answer)
Understanding slicing
(38 answers)
Closed 6 months ago.
X = df_census.iloc[:,:-1]
y = df_census.iloc[:,-1]
what is the meaning of [:,:-1] in this and also [:,-1]
Following your example: [:,:-1]
The first argument is : which means to get all rows of the dataframe, and :-1 means to return all columns but the last one
In the case of just -1, it means to get the last column

Writing to csv makes column without being written [duplicate]

This question already has an answer here:
Pandas to_csv call is prepending a comma
(1 answer)
Closed 1 year ago.
I am trying to do math on a specific cell in a csv file with pandas, but whenever I try to do it, it adds a column on the left that looks like
,
1,value,value
2,value,value
3,value,value
4,value,value
5,value,value
So if I run it multiple times it looks like
,
1,1,1,value,value
2,2,2,value,value
3,3,3,value,value
4,4,4,value,value
5,5,5,value,value
How do I make it not do this??
My code is: add_e_trial = equations_reader.at[bank_indexer_addbalance, 1] = reciever_amount
Whenever you do a df.to_csv, include index=False. The left most column is the index of the dataset
df.to_csv(index=False)

How to assign a variable in a column given another variable? [duplicate]

This question already has answers here:
Conditional Replace Pandas
(7 answers)
Adding a new pandas column with mapped value from a dictionary [duplicate]
(1 answer)
Closed 3 years ago.
This is my dataset :
Datset example
I want to assign to the different values* such as 'W M I HOLDINGS CORP' and 'MILESTONE SCIENTIFIC INC' another variable in the column 'ticker' in order to have the opportunity to sort them.
In the column 'ticker' I need to add WMIH and WLSS respectively to the two different values.
How can I do that ?
I am going to expect the output with something like this :
Output Example
You should be ok with this. Assuming you have your dataset in df variable.
df.loc[df['comnam'] == 'W M I HOLDINGS CORP', 'ticker'] = 'WMIH'
df.loc[df['comnam'] == 'MILESTONE SCIENTIFIC INC', 'ticker'] = 'WLSS'

change column value based on multiple conditions

I've seen a lot of posts similar but none seem to answer this question:
I have a data frame with multiple columns. Lets say A, B and C
I want to change column A's value based on conditions on A, B and C
I've got this so far but not working.
df=df.loc[(df['A']=='Harry')
& (df['B']=='George')
& (df['C']>'2019'),'A']=='Matt'
So if A is equal to Harry, and B is equal to George and C is greater than 2019, then change A to Matt
Anyone see what I've done wrong?
You are really close, assign value Matt to filtered A by boolean masks:
df.loc[(df['A']=='Harry') & (df['B']=='George') & (df['C']>'2019'),'A'] = 'Matt'
You can also use np.where
df['A'] = np.where((df['A']=='Harry') & (df['B']=='George') & (df['C']>'2019'), 'Matt', df['A'])

Check if pandas column contains all zeros [duplicate]

This question already has answers here:
How do I delete a column that contains only zeros in Pandas?
(4 answers)
Closed 5 years ago.
I'm doing a complex calculation on a data frame that is bound to throw exceptions if all the values in a column are zeros. How to do a quick check whether a column is full of zero? i.e. return True if the column has values other than 0 else False.
you can do something like this:
(df['col'] == 0).all()
This will return True if all values are 0 otherwise it will return false