Frequency count for each column in pandas dataset using a generalised approach [duplicate] - pandas

This question already has answers here:
Get total of Pandas column
(5 answers)
Pandas: sum DataFrame rows for given columns
(8 answers)
Closed 4 years ago.
I have a pandas dataframe with Binary values in the columns as such.
Id Label1 Label2 ......
1 0 1
2 1 1
3 1 1
4 1 1
The output I am looking for is as follows
Label1
3
Label2
4
How do I do this using some form of code that reads each of the columns in the whole panda data-frame and provides the sum for that column. I do know how to do it for one column.

Related

pandas how to explode from two cells element-wise [duplicate]

This question already has answers here:
Efficient way to unnest (explode) multiple list columns in a pandas DataFrame
(7 answers)
Closed 9 months ago.
I have a dataframe:
df =
A B C
1 [2,3] [4,5]
And I want to explode it element-wise based on [B,C] to get:
df =
A B C
1 2 4
1 3 5
What is the best way to do so?
B and C are always at the same length.
Thanks
Try, in pandas 1.3.2:
df.explode(['B', 'C'])
Output:
A B C
0 1 2 4
0 1 3 5

Groupby and count of other column with condition [duplicate]

This question already has answers here:
Groupby value counts on the dataframe pandas
(6 answers)
Closed 2 years ago.
I have a dataframe:
id value
A 1
A 0
A 1
B 1
B 1
C 0
I'm trying to group by id and count occurances in value column such that I count no. of ones and no. of 0's in each group:
id No. of 1's No of 0's
A 2 1
B 1 0
C 0 1
I know of a way to groupby and use aggregate function
df.groupby('id').agg({'value': xxx})
But i think there should be a much better way to do this.
you need unstack and add_prefix/suffix
df.groupby(["id", "value"])["value"].count().unstack(1).fillna(0).add_prefix(
"counts_of_"
).add_suffix("'s")
value counts_of_0's counts_of_1's
id
A 1.0 2.0
B 0.0 2.0
C 1.0 0.0

Dataframe group by numerical column and then combine with the original dataframe [duplicate]

This question already has answers here:
Pandas new column from groupby averages
(2 answers)
Closed 2 years ago.
I have a pandas data frame and I would like to first group by one of the columns and calculate mean of count of each group of that column. Then, I would like to combine this grouped entity with the original data frame.
An example:
df =
a b orders
1 3 5
5 8 10
2 3 6
Group by along column b and taking mean of orders
groupby_df =
b mean(orders)
3 5.5
8 10
End result:
df =
a b orders. mean(orders)
1 3 5 5.5
5 8 10 10
2 3 6 5.5
I know I can group by on b and then, do a inner join on b, but, I feel like it can be done in much cleaner/one-liner way. Is it possible to do better than that?
This is transform
df['mean']=df.groupby('b').orders.transform('mean')

calculate the mean of a column based on the label in a pandas dataframe [duplicate]

This question already has answers here:
Pandas Mean for Certain Column
(4 answers)
Closed 2 years ago.
Actually I am new to python and am facing some problems with the pandas dataframe. I want to find out the mean of the columns that have a label positive. I have three columns x1, x2 and label. I want to find out the mean of x1 which have the label 'positive'. I have used a pandas dataframe which looks like this. Can someone help me with this.
x1 x2 label
0 5 2 positive
1 6 1 positive
2 7 3 positive
3 7 5 positive
4 8 10 positive
5 9 3 positive
6 0 4 negative
7 1 8 negative
8 2 6 negative
9 4 10 negative
10 5 9 negative
11 6 11 negative
You may want to look at df.loc[] after filtering with df['label'].eq('positive'):
df.loc[df['label'].eq('positive'),'x1'].mean()
You can do it using boolean indexing as follows:
df.loc[df['label'] == 'positive', 'x1'].mean()
or alternatively
df.loc[df['label'].isin(['positive']), 'x1'].mean()
The boolean indexing array is True for the correct clusters. x1 is just the name of the column to compute the mean over.

flattern pandas dataframe column levels [duplicate]

This question already has answers here:
Pandas: combining header rows of a multiIndex DataFrame
(1 answer)
How to flatten a hierarchical index in columns
(19 answers)
Closed 4 years ago.
I'm surprised, i haven't found anything relevant.
I simply need to flattern this DataFrame with some unifying symbol, e.g. "_".
So, i need this
A B
a1 a2 b1 b2
id
264 0 0 1 1
321 1 1 2 2
to look like this:
A_a1 A_a2 B_b1 B_b2
id
264 0 0 1 1
321 1 1 2 2
Try this:
df.columns = df.columns.map('_'.join)