This question already has answers here:
how to sort pandas dataframe from one column
(13 answers)
Closed 3 years ago.
rating
size mean
title
'Til There Was You (1997) 9 2.333333
1-900 (1994) 5 2.600000
101 Dalmatians (1996) 109 2.908257
12 Angry Men (1957) 125 4.344000
187 (1997) 41 3.024390
How can i sort based on mean column?
As it is a MultiIndex DataFrame, you could do:
>>> df.sort_values([('rating', 'mean')])
Related
This question already has answers here:
Use groupby in Pandas to count things in one column in comparison to another
(4 answers)
How can I pivot a dataframe?
(5 answers)
Closed 1 year ago.
I have my dataframe say df
data = [['00637', 'rew_A'], ['5644', 'rew_A'], ['564', 'rew_A'],
['2218', 'rew_C'], ['990', 'rew_C'], ['17', 'rew_A'],
['5565', 'rew_C'], ['121', 'rew_A'], ['76700', 'rew_B'],
['00637', 'rew_C']]
t = pd.DataFrame(data, columns = ['emp_id', 'reward'])
t
emp_id reward
0 00637 rew_A
1 5644 rew_A
2 564 rew_A
3 2218 rew_C
4 990 rew_C
5 17 rew_A
6 5565 rew_C
7 121 rew_A
8 76700 rew_B
9 00637 rew_C
My OP should contain 4 columns i.e - emp_id, rew_A, rew_B, and rew_C, a basic pivot table which should look like -
emp_id rew_A rew_B rew_C
0 00637 1 1
1 5644 1
2 564 1
Please help me out to create this.
Thanks!!! :)
This question already has answers here:
How can I pivot a dataframe?
(5 answers)
Closed 3 years ago.
I want to transform distinct rows in a dataframe into columns and the values assigned to each column.
I have a pandas dataframe with this structure (coming from a json file):
Key Value
0 _id 1
1 type house
2 surface 156
3 county andr
4 _id 2
5 type apartment
6 surface 95
7 county sprl
8 _id 3
9 type house
10 surface 234
11 county ilm
..
I expect a dataframe similar to:
_id type surface county
0 1 house 156 andr
1 2 apartment 95 sprl
2 3 house 234 ilm
...
df = pd.read_json(your_json, orient='records')
This should read it in the format you want.
This question already has answers here:
How can I pivot a dataframe?
(5 answers)
Closed 3 years ago.
I want to create a dataframe that takes the 1st index and makes it into a column.
My grouping code:
candy_df.groupby(['BAG', 'LOLLIPOP']).agg('count')['STICKID']
Right now my grouping returns this:
BAG LOLLIPOP
011111 CHOCO 69
VANILL 33
011112 CHOCO 133
VANILL 129
I'd like to take the 1st index, LOLLIPOP, and make the different flavors be the columns:
BAG CHOCO VANILL
011111 69 33
011112 133 129
candy_df.groupby(['BAG', 'LOLLIPOP'])['STICKID'].count().unstack()
This question was also answered under Question 4 under How to pivot a dataframe
This question already has answers here:
Pandas groupby with delimiter join
(2 answers)
pandas groupby concatenate strings in multiple columns
(1 answer)
Closed 4 years ago.
I am dealing with a data set which has the following fields:
ID Person_Name Person_Country
110 Marc CA
110 Sean CN
111 Matt IN
111 Rob AU
112 Mike US
I intend grouping the data in the following way:
ID Person_Name Person_Country
110 Marc; Sean CA; CN
111 Matt; Rob IN; AU
112 Mike US
I tried using the built-in functions like .pivot_table() and .unstack(), but they weren't helpful since I am dealing with non-numeric data.
This question already has answers here:
Increase index of pandas DataFrame by one
(2 answers)
Closed 6 months ago.
Currently I am trying to read in a .csv file and then use the to_html() to create a table with indexing on the side. All lines of code here:
import pandas as pd
df = pd.read_csv('file.csv')
df.to_html('example.html')
As expected I am currently getting:
Year Population Annual Growth Rate
0 1950 2557628654 1.458
1 1951 2594919657 1.611
2 1952 2636732631 1.717
3 1953 2681994386 1.796
4 1954 2730149884 1.899
However I want to start the indexing at 2 instead of 0. For example:
Year Population Annual Growth Rate
2 1950 2557628654 1.458
3 1951 2594919657 1.611
4 1952 2636732631 1.717
5 1953 2681994386 1.796
6 1954 2730149884 1.899
I know I could achieve this outcome by adding two dummy rows in the .csv file and then deleting them with df.ix[], but I do not want to do this.
Is there a way to change the indexing to start at something other than 0 without having to add or delete rows in the .csv file?
Thanks!
I know it looks like a hack, but what if just change index series. For example:
df.index = df.index + 2