How can I create a new data frame based on the existing columns?

nurer

How can I create a new data frame based on the existing columns? It should calculate the average of the column 'a' for each same x. For example: a_new = sum the 'a' values and divide 3 where x=1. And also, for x=2, x=3,....

import pandas as pd
data = {'x': [ 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], 'a': [0.4, 0.88, 0.2, 0.1, 0.75, 0.98, 0.33, 0.22, 0.15, 0.14, 0.73, 0.25], 'year': [2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002]}   
df = pd.DataFrame(data)
df

    x    a      year
0   1   0.40    2000
1   2   0.88    2000
2   3   0.20    2000
3   4   0.10    2000
4   1   0.75    2001
5   2   0.98    2001
6   3   0.33    2001
7   4   0.22    2001
8   1   0.15    2002
9   2   0.14    2002
10  3   0.73    2002
11  4   0.25    2002

Expected Output:

    x   a_new
0   1   0.30
1   2   0.66
2   3   0.42
3   4   0.19
optical_anathema

This might be what you're after.

df.groupby(['x']).mean()['a']
x
1    0.433333
2    0.666667
3    0.420000
4    0.190000
Name: a, dtype: float64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Create new columns in a data frame based on an existing numeric column, a list of strings as column names and a list of tuples as values

How can I rename all columns of a data frame based on another data frame in R?

How Can I create three new columns for my data?

How to create a new dataframe with values based on existing data frame and range from numeric vectors in R.

How do I create columns in a data frame based on a date range?

How do I create new columns by combining data in existing columns?

How can I create a new series by using specific rows and columns of a pandas data frame?

How can I create a new category in pandas data frame based on my previous categories?

Calculating new column in a data frame based on existing columns

Calculating a new accumulative column in a data frame based on existing columns

How to create new columns based on values and names of existing columns in R?

How can I create a new data frame with several rows for each observation based on string column?

Creating a new data frame from two existing data frame based on values from two columns

How do I create a new pandas dataframe after comparing 2 data frame columns based on a condition?

how do I create a new column in my main data frame filling in the values from a smaller dataset based on two columns they have in common?

Creating a new accumulative column in a data frame based on existing columns

How can create a new column in Google BigQuery with SQL that is based on a calculation of existing columns?

How can I create a data frame with all existing variables (at once)?

How can I filter into a new data frame conditional on the columns existing in another data frame?

Create a Dynamic Number of New Columns in a Data Frame Based on the Levels in a Vector

create a new data frame from existing data frame based on condition

Create new columns based on count of occurrence in another data frame in r

How can I create a new wide data frame with rows based on all combos of values in two columns?

How to create new data frame based on conditions

Trying to write a function that takes a data frame and a column to create a new numeric column based on the existing columns. Only NA is coming,

Reshaping Panda Data Frame and Create New Columns Based On a Column

How to create new columns in a new data frame from information in an existing data frame in R

how can I create a new data frame using exact rows from the old data frame in R Studio?

Create new Column based on the data of existing columns