removing rows in pandas based on a column's values [duplicate] - python

This question already has answers here:
Use a list of values to select rows from a Pandas dataframe
(8 answers)
Filter dataframe rows if value in column is in a set list of values [duplicate]
(7 answers)
Closed 5 years ago.
This is a subset of a dataframe:
index drug_id values
1 le.1 f
2 le.7 h
3 le.10 9
4 le.11 10
5 le.15 S
I am going to remove rows that values in the drug_id column are: le.7, le.10, le.11.
This is my code:
df.drop(df.drug_id[['le.7', 'le.10', 'le.11']], inplace = True )
I also tried this:
df.drop(df.drug_id == ['le.7', 'le.10', 'le.11'], inplace = True )
But none of them worked. Any suggestion ?

Related

booleen mask for datatypes using pandas? [duplicate]

This question already has answers here:
How to drop rows of Pandas DataFrame whose value in a certain column is NaN
(15 answers)
Closed 6 months ago.
i know we can use say df['col'] >= 1 to return a booleen mask of true/false values for a specific pandas df column, but is there a way to filter for data types?
I'd like to filter out NaN values in a column that has both string and NaN values.
You can find the NaN's with df['col'].isna(). Returns a boolean mask.

Select rows with conditions based on two columns(Start date and end date) [duplicate]

This question already has answers here:
pandas: multiple conditions while indexing data frame - unexpected behavior
(5 answers)
Pandas slicing/selecting with multiple conditions with or statement
(1 answer)
Closed 2 years ago.
I have a dataframe which looks like this:
id start_date end_date
0 1 2017/06/01 2021/05/31
1 2 2018/10/01 2022/09/30
2 3 2015/01/01 2019/02/28
3 4 2017/11/01 2021/10/31
Can anyone tell me how i will slice the rows only for the start date which is 2017/06/01 and end date which is 2021/10/31 only.

Transform the row to a column and count the occurrence by doing a group by [duplicate]

This question already has answers here:
Pandas, Pivot table from 2 columns with values being a count of one of those columns
(2 answers)
Most efficient way to melt dataframe with a ton of possible values pandas
(2 answers)
How to form a pivot table on two categorical columns and count for each index?
(2 answers)
Closed 2 years ago.
am trying to transform the rows and count the occurrences of the values based on groupby the id
Dataframe:
id value
A cake
A cookie
B cookie
B cookie
C cake
C cake
C cookie
expected:
id cake cookie
A 1 1
B 0 2
c 2 1

how to truncate certain columns to X number of characters? [duplicate]

This question already has answers here:
Pandas dataframe: truncate string fields
(4 answers)
Closed 4 years ago.
I have a dataframe with some columns having large sentences.
How do I truncate the columns to say 50 characters max?
current df:
a b c
I like data science 1 2
new truncated df for ONLY column a:
a b c
I like data 1 2
(The above is an example sentence I made up)
For a specific column:
df['a'] = df['a'].str[:50]

changing row values of pandas from dictionary [duplicate]

This question already has answers here:
Remap values in pandas column with a dict, preserve NaNs
(11 answers)
Replace values in a pandas series via dictionary efficiently
(1 answer)
Closed 4 years ago.
I have a dataframe where i need to change row values if it is present in a dictionary like this:
dict = {"A":Apple:"B":Ball,"C":Cat}
c1 c2 c3
0 A Tree GH
1 B Train GC
2 C Yarn GR
I want the column c1 values to be changed from the dict if it is present.

Categories

Resources