Pandas groupby and append the original values. Count the mean of per row

Ahsan

I have a dataframe of IDs and Values. Where IDs are kind of repetition of trial and Values are the results. I want to do groupby by ID and for same IDs the Values will be added to adjacent columns. Finally I want to calculate the mean of each of the rows.

>>>df
   ID  Value
0   1    1.1
1   2    1.2
2   3    2.4
3   1    1.7
4   2    4.3
5   3    2.2
>>>groups = df.groupby(by='ID')

#Now I cannot figure it what to do for my desired output.

I want the output like

   ID  Value_1  Value_2  Mean
0   1    1.1    1.7    1.9
1   2    1.2    4.3    2.75
2   3    2.4    2.2    2.3
jezrael

Use DataFrame.assign for new column created by counter per groups by GroupBy.cumcount, reshape by DataFrame.pivot, change columns names by DataFrame.add_prefix, add new column filled by means and last data cleaning - DataFrame.reset_index with DataFrame.rename_axis:

df = (df.assign(g = df.groupby('ID').cumcount().add(1))
        .pivot('ID','g','Value')
        .add_prefix('Value_')
        .assign(Mean = lambda x: x.mean(axis=1))
        .reset_index()
        .rename_axis(None, axis=1))
print (df)
   ID  Value_1  Value_2  Mean
0   1      1.1      1.7  1.40
1   2      1.2      4.3  2.75
2   3      2.4      2.2  2.30

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unique Values per row in Pandas groupby

From Dev

python - pandas: count identical values per row

From Dev

Pandas count true boolean values per row

From Dev

Pandas Series of lists of strings how to count and append to df per row

From Dev

Pandas; calculate mean and append mean to original frame

From Dev

Get count of non zero values per row in Pandas DataFrame

From Java

Pandas Groupby: Count and mean combined

From Dev

pandas groupby count and then conditional mean

From Dev

Count NaN per row with Pandas

From Dev

Calculate mean for only three values per row

From Dev

Groupby count values isin - pandas

From Dev

PANDAS groupby 2 columns then count and mean

From Dev

Pandas groupby calculate mean of every nth row

From Dev

Pandas: Groupby to create table with count and count values

From Dev

Count number of unique values per row

From Dev

Count of values per row that do not satisfy a condition

From Dev

Get Pandas Duplicate Row Count with Original Index

From Dev

Pandas groupby mean of only positive values

From Dev

Pandas: How to fill null values with mean of a groupby?

From Dev

append specific column value from original DataFrame after groupby - Pandas

From Dev

Pandas: Count values on a row basis

From Dev

Pandas count values by condition for row

From Dev

Pandas count recurring values per week

From Java

Count of values grouped per month, year - Pandas

From Java

Count unique values with pandas per groups

From Dev

Pandas - count distinct values per column

From Dev

append value of next row with current row pandas groupby id

From Java

Top 3 Values Per Row in Pandas

From Dev

Pandas : vectorized operations on maximum values per row

Related Related

HotTag

Archive