Dataframe add new rows from existing ones

fanbondi

My current data:

Type             Country        Score
University       Australia       10
University       Brazil          10
University       Hong Kong       10
College          Australia       10
College          Brazil          10
College          Hong Kong       10

Now, I want to have a summary of new rows created from the above data as shown below. The new rows are a sum of the scores for each university and college in a country.

Type             Country         Score
University       Australia       10
University       Brazil          10
University       Hong Kong       10
College          Australia       10
College          Brazil          10
College          Hong Kong       10
All              Australia       20
All              Brazil          20
All              Honk Kong       20

I know I can write a loop to iterate over the data and check the countries but maybe I can use packages like dplyr to achieve what I want.

akrun

We need to group_by 'Country' get the sum of 'Score', create a new column 'Type' with "All" and bind the rows with original data

library(dplyr)
df1  %>% 
  group_by(Country) %>%
  summarise(Score = sum(Score)) %>%
  mutate(Type = "All") %>% 
  bind_rows(df1, .)
#        Type   Country Score
#1 University Australia    10
#2 University    Brazil    10
#3 University Hong Kong    10
#4    College Australia    10
#5    College    Brazil    10
#6    College Hong Kong    10
#7        All Australia    20
#8        All    Brazil    20
#9        All Hong Kong    20

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Add new rows to calculate the sum and average from exiting pandas dataframe

分類Dev

MVC5 - ManytoMany Using ApplicationUser Adds new Rows instead of using Existing Ones / Or Throws Error

分類Dev

Creating a new dataframe with many rows for each row in existing dataframe

分類Dev

Create a new DataFrame from two existing DataFrames

分類Dev

Pandas - Add new rows to dataframe with arithmetic

分類Dev

How to add new grid rows without clearing existing data

分類Dev

Sqlalchemy: add into mysql table new rows from pandas dataframe, if they don't exist already in the table

分類Dev

How to pass schema to create a new Dataframe from existing Dataframe?

分類Dev

Creating new pandas DataFrame from existing DataFrame and index

分類Dev

Add new column to dataframe depending on interqection of existing columns with pyspark

分類Dev

add groupby to existing dataframe

分類Dev

Create a new DataFrame column from a condition of an existing one?

分類Dev

Creating new class instances inside an array without overwriting existing ones

分類Dev

xslt - adding new xml tags by modifying existing ones

分類Dev

pandas dataframe - add new row if new index, if existing then supplement the index with column data

分類Dev

Add new Validator to Existing Collection

分類Dev

Add n empty rows in a dataframe

分類Dev

Is it possible to add a new field to an existing field of RECORD type in bigquery from UI?

分類Dev

How to add a new key value pair to existing key value pair from list of dicts?

分類Dev

Swift JSON add new key to existing dictionary

分類Dev

Add new environment to an existing release-build

分類Dev

How to add a new column to an existing entity with typeorm

分類Dev

Add new array values to existing array position

分類Dev

Dynamically add new rows and new sub rows using jquery

分類Dev

Add multiple dataframes to one dataframe without overwriting the existing dataframe in R

分類Dev

Merge Multiple Columns As New Rows in Pandas Dataframe

分類Dev

Create new Columns based on values in existing Columns and merge Rows

分類Dev

Python: create a new column from existing columns

分類Dev

Creating a new MySQL replica from an existing replica

Related 関連記事

  1. 1

    Add new rows to calculate the sum and average from exiting pandas dataframe

  2. 2

    MVC5 - ManytoMany Using ApplicationUser Adds new Rows instead of using Existing Ones / Or Throws Error

  3. 3

    Creating a new dataframe with many rows for each row in existing dataframe

  4. 4

    Create a new DataFrame from two existing DataFrames

  5. 5

    Pandas - Add new rows to dataframe with arithmetic

  6. 6

    How to add new grid rows without clearing existing data

  7. 7

    Sqlalchemy: add into mysql table new rows from pandas dataframe, if they don't exist already in the table

  8. 8

    How to pass schema to create a new Dataframe from existing Dataframe?

  9. 9

    Creating new pandas DataFrame from existing DataFrame and index

  10. 10

    Add new column to dataframe depending on interqection of existing columns with pyspark

  11. 11

    add groupby to existing dataframe

  12. 12

    Create a new DataFrame column from a condition of an existing one?

  13. 13

    Creating new class instances inside an array without overwriting existing ones

  14. 14

    xslt - adding new xml tags by modifying existing ones

  15. 15

    pandas dataframe - add new row if new index, if existing then supplement the index with column data

  16. 16

    Add new Validator to Existing Collection

  17. 17

    Add n empty rows in a dataframe

  18. 18

    Is it possible to add a new field to an existing field of RECORD type in bigquery from UI?

  19. 19

    How to add a new key value pair to existing key value pair from list of dicts?

  20. 20

    Swift JSON add new key to existing dictionary

  21. 21

    Add new environment to an existing release-build

  22. 22

    How to add a new column to an existing entity with typeorm

  23. 23

    Add new array values to existing array position

  24. 24

    Dynamically add new rows and new sub rows using jquery

  25. 25

    Add multiple dataframes to one dataframe without overwriting the existing dataframe in R

  26. 26

    Merge Multiple Columns As New Rows in Pandas Dataframe

  27. 27

    Create new Columns based on values in existing Columns and merge Rows

  28. 28

    Python: create a new column from existing columns

  29. 29

    Creating a new MySQL replica from an existing replica

ホットタグ

アーカイブ