Python pandas. how to delete date rows by condition? [duplicate] - python

This question already has answers here:
How do I select rows from a DataFrame based on column values?
(16 answers)
Pandas filter dataframe rows with a specific year
(2 answers)
Closed 4 years ago.
i have a dataframe with dates as index, dates from 2013 year to 2018 year
how can i delete all rows where year < 2018?
enter image description here

Related

How do you convert a date to a number? [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 1 year ago.
How do you convert a pandas dataframe column from a date formatted as below to a number as shown below:
date
0 4/5/2010
1 9/26/2014
2 8/3/2010
To this
date newFormat
0 4/5/2010 40273
1 9/26/2014 41908
2 8/3/2010 40393
Where the second columns is the number of days since 1/1/1900.
Use:
data['newFormat'] = data['Date'].dt.strftime("%Y%m%d").astype(int)
This has been answered before:
Pandas: convert date 'object' to int
enter link description here

pandas select rows with no duplicate [duplicate]

This question already has answers here:
Remove pandas rows with duplicate indices
(7 answers)
Closed 2 years ago.
I have 1 dataframe and I want to select all rows that don't have duplicates
My df:
Name Age
Jp 4
Anna 15
Jp 4
John 10
My output should be :
Name Age
Anna 15
John 10
I am using Pandas dataframe
any suggestions?
You want to drop duplicates across multiple columns:
df.drop_duplicates(['Name','Age'])
Please see the pandas documentation on basic methods of dataframes.

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

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

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 ?

Categories

Resources