Regrouping sum all duplicate Value in Panda [duplicate] - python

This question already has answers here:
Pandas Groupby and Sum Only One Column
(3 answers)
Closed 2 years ago.
I have a dataframe in Panda with the number of cows stolen per Year :
stolen_each_year = data[['stolen cows','year_of_date' ]]
I would like to remove all Duplicate years and keep just one with the sum of all.
I have an idea with a python function but I am trying to use the panda at the maximum
Thank You for your time
EDIT : I tried with the .groupby method but it does not seem to work fine

You can groupby and then sum the stolen cows.
stolen_each_year.groupby("year_of_date")['stolen cows'].sum()
also... interesting dataset 🐮...

Related

Sort part datframe decreasing and an another part increasing [duplicate]

This question already has answers here:
How to sort a dataFrame in python pandas by two or more columns?
(3 answers)
Closed 4 months ago.
In a data frame with 6 column a,b,c,d,e,f
i want to sort a,b,c by a (ascending) and d,e,f by f (descending)
I don't really know the easy way out but you could use this until someone point it out.
df_desc=self.orderbook_agreg_btc[["bids_qty","bids_price","exchange_name_bid"]].sort_values(["bids_price"],ascending= False)
df_asc=self.orderbook_agreg_btc[["asks_qty","asks_price","exchange_name_ask"]].sort_values(["asks_price"],ascending= True)
df = df_desc.append(df_asc)

How to transpose values in one column to columns using Pandas? [duplicate]

This question already has answers here:
How to pivot a dataframe in Pandas? [duplicate]
(2 answers)
Closed 1 year ago.
Hi there I have a data set look like df1 below and I want to make it look like df2 using pandas. I have tried to use pivot and transpose but can't wrap my head around how to do it. Appreciate any help!
This should do the job
df.pivot_table(index=["AssetID"], columns='MeterName', values='MeterValue')
index: Identifier
columns: row values that will become columns
values: values to put in those columns
I often have the same trouble:
https://towardsdatascience.com/reshape-pandas-dataframe-with-pivot-table-in-python-tutorial-and-visualization-2248c2012a31
This could help next time.

Using Python, Get Partial Sum of DF column based on a condition from another sorted column [duplicate]

This question already has answers here:
How do I Pandas group-by to get sum?
(11 answers)
Closed 2 years ago.
DataFrame in question
I need to find out total invoice value for each supplier and create a new dataframe with unique supplier names as follows.
Final Output desired
Try this:
sum_by_supplier = (df.groupby('Supplier Name')['Invoice Value'].sum()).reset_index()

How to calculate word frequency of a column in a csv file? [duplicate]

This question already has answers here:
Count frequency of values in pandas DataFrame column
(5 answers)
Count and Sort with Pandas
(5 answers)
Closed 5 years ago.
I am new with Python but I would like to see the frequency of words used in titles of games, which are in the first column named "Name" (from a dataset of Kaggle "Video Games with Ratings").
Thus, I am trying to have a sort of a list with "word" & frequency
(e.g: "hero" : 4
"pokemon": 8)
I tried to use most.common() but it doesn't work. Could someone help me and comment their answer ?
Thank you and have a nice day/evening !

Get specific element from Groups after applying groupby - PANDAS [duplicate]

This question already has answers here:
How to access pandas groupby dataframe by key
(6 answers)
Closed 9 years ago.
I have grouped sum data in a dataframe, by the following:
groups = df.groupby(['name'])
Now I can get the head of the groups by groups.head(2) which gives the first two rows.
But how do I get a group by a specific name? i.e. if I want the single group where the group name is 'ruby', I can't just do groups['ruby']
How about:
groups.get_group('name')
For more elaboration, see this related question

Categories