Droping a range of rows in pandas [duplicate] - python

This question already has answers here:
How to drop a list of rows from Pandas dataframe?
(15 answers)
Closed 3 years ago.
I want to clear the first 9 rows of a dataframe in Pandas.
I have to drop all rows using:
df.drop([0,1,2,3,4,5,6,7,8])
Is there a more efficient way to do this?
I have tried using a range:
df.drop([0:9])
but this does not help.

The best way to do this is by indexing: df.drop(df.index[0:9]). This example will drop the first ten rows.

Related

split a column to two column with duplicates on the ID using python [duplicate]

This question already has answers here:
Max and Min date in pandas groupby
(3 answers)
Closed 4 months ago.
enter image description here
Hello, does someone have an idea to help to split a column in two columns. I have duplicates in my Id columns so i have some difficulties if someone can lay me some track, i would appreciate a lot.
there is a png to illustrate my situation
You can use the groupby function and then aggregate the values.
df = df.groupby('Id').agg({'Date': ['min', 'max']}).reset_index()

Python: transpose and group dataframe [duplicate]

This question already has answers here:
How can I pivot a dataframe?
(5 answers)
Closed 2 years ago.
I have dataframe: table_revenue
how can I transpose the dataframe and have grouping by 'stations_id' to see final result as:
where values of cells is the price, aggregated by exact date (column) for specific 'station_id' (row)
It seems you need pivot_table():
output = input.pivot_table(index='station_id',columns='endAt',values='price',aggfunc='sum',fill_value=0)

How to select and extract the rows with the same ID which has the minimum value in one column in jupyter? [duplicate]

This question already has answers here:
Keep other columns when doing groupby
(5 answers)
Closed 3 years ago.
I have a data frame that has some rows with the same ID which includes different starcounter values.
I need to keep the rows with a minimum value and delete the extra rows to reach this table:
.
Thank you in advance.
What you need is
df2 = df.sort_values('starcounter').drop_duplicates(['ID'], keep='first')
Here's a one liner to do this:
df.loc[df.groupby('ID')['starcounter'].idxmin()]

Selecting rows in pandas for where a column is equal to certain values [duplicate]

This question already has answers here:
Filter dataframe rows if value in column is in a set list of values [duplicate]
(7 answers)
Closed 4 years ago.
I have a pandas dataframe that looks like this:
From this, I want to grab all the rows for particular Filters (1st column). So for example, I want to grab the rows for F218W, F336W, and F373N.
What is the easiest way to do this in pandas?
In addition if I wanted to grab the rows for those filters but also only for Chip 1, how could I do that easily?
Thanks!
This is a simple slicing:
df[df["# Filter"].isin(["F218W", "F336W","F373N"])]
If the rules across multiple columns, you can simply combine them using &:
df[df["# Filter"].isin(["F218W", "F336W","F373N"]) & (df["Chip"] == 1)]

Pandas: Return a new Dataframe with specific non continuous column selection [duplicate]

This question already has answers here:
How to take column-slices of dataframe in pandas
(11 answers)
Closed 6 years ago.
I have a dataframe with 85 columns and something like 10.000 rows.
The first column is Shrt_Desc and the last Refuse_Pct
The new data frame that I want has to have Shrt_Desc, then leave some columns out and then include in series Fiber_TD_(g) to Refuse_Pct
I use:
dfi_3 = food_info.loc[:, ['Shrt_Desc', 'Fiber_TD_(g)':'Refuse_Pct']]
but it gives a syntax error.
Any ideas how can I achieve this?
Thank you.
Borrowing the main idea from this answer:
pd.concat([food_info['Shrt_Desc'], food_info.ix[:, 'Fiber_TD_(g)':]], axis=1)

Categories

Resources