How to apply value_counts(normalize=True) and value_counts() to pandas series?

HedgeHog

I like to show the value_counts(normalize=True) of a series what works well, but I also wanna show the value_counts() not normalized in an additional column.

Code

import pandas as pd

cars = {'Brand': ['Honda Civic','Toyota Corolla','','Audi A4'],
        'Price': [32000,35000,37000,45000]
        }

df = pd.DataFrame(cars, columns = ['Brand', 'Price'])

df.Brand.value_counts(normalize=True)

Expected output

                  perc   count
Toyota Corolla    0.25   1
Audi A4           0.25   1
Honda Civic       0.25   1
                  0.25   1
Name: Brand, dtype: float64

Question

How could I attache both information to the series?

jezrael

If want use value_counts you need run code without normalize=True:

df = pd.concat([df.Brand.value_counts(normalize=True),
                df.Brand.value_counts()], 
                axis=1,
                keys=('perc','count'))
print (df)
                perc  count
                0.25      1
Honda Civic     0.25      1
Toyota Corolla  0.25      1
Audi A4         0.25      1

Another idea is create perc column in another step, DataFrame.insert is for set position of new column:

df = df.Brand.value_counts().to_frame('count')
df.insert(0, 'perc', df['count'].div(len(df)))
         
print (df)
                perc  count
                0.25      1
Honda Civic     0.25      1
Toyota Corolla  0.25      1
Audi A4         0.25      1

df = df.Brand.value_counts(normalize=True).to_frame('perc')
df['count'] = df['perc'].mul(len(df))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas Series value_counts working differently for different counts

From Dev

how to get return value of value_counts() in pandas with right order

From Dev

How to do some operations like groupby() and value_counts() in pandas?

From Dev

Pandas value_counts(sort=False) with large series doesn't work

From Dev

How to plot a value_counts in pandas that has a huge number of different counts not distributed evenly

From Dev

Pandas: inverse of value_counts function

From Java

Extract values in Pandas value_counts()

From Dev

Explanation about pandas value_counts function

From Dev

pandas value_counts applied to each column

From Dev

Pandas value_counts Into New Columns

From Dev

Using Pandas Value_Counts and matplotlib

From Dev

Using value_counts in pandas with condtions

From Dev

Accessing first column of pandas value_counts

From Dev

Pandas groupby into unique sets and then value_counts

From Dev

Corresponding indices of pandas value_counts() method

From Dev

Adding a 'rest'-group with Pandas value_counts()

From Dev

Pandas: value_counts and cut with groupby multiindex

From Dev

python/pandas - Transformed value_counts by Category

From Dev

pandas value_counts with bins argument

From Java

Python Pandas: Convert ".value_counts" output to dataframe

From Dev

Change values in pandas dataframe according to value_counts()

From Dev

Pandas reduce number of categorical variables in value_counts() tabulation

From Dev

pandas sparse data frame value_counts not working

From Dev

pandas value_counts() with multiple values in list form?

From Dev

Not getting 0 index from pandas value_counts()

From Dev

Group by and find top n value_counts pandas

From Dev

Leading or trailing whitespace and pandas value_counts vs boolean selection

From Dev

pandas value_counts with bins applied to a groupby produces incorrect results

From Dev

Python Pandas: Convert ".value_counts" output to dataframe

Related Related

  1. 1

    Pandas Series value_counts working differently for different counts

  2. 2

    how to get return value of value_counts() in pandas with right order

  3. 3

    How to do some operations like groupby() and value_counts() in pandas?

  4. 4

    Pandas value_counts(sort=False) with large series doesn't work

  5. 5

    How to plot a value_counts in pandas that has a huge number of different counts not distributed evenly

  6. 6

    Pandas: inverse of value_counts function

  7. 7

    Extract values in Pandas value_counts()

  8. 8

    Explanation about pandas value_counts function

  9. 9

    pandas value_counts applied to each column

  10. 10

    Pandas value_counts Into New Columns

  11. 11

    Using Pandas Value_Counts and matplotlib

  12. 12

    Using value_counts in pandas with condtions

  13. 13

    Accessing first column of pandas value_counts

  14. 14

    Pandas groupby into unique sets and then value_counts

  15. 15

    Corresponding indices of pandas value_counts() method

  16. 16

    Adding a 'rest'-group with Pandas value_counts()

  17. 17

    Pandas: value_counts and cut with groupby multiindex

  18. 18

    python/pandas - Transformed value_counts by Category

  19. 19

    pandas value_counts with bins argument

  20. 20

    Python Pandas: Convert ".value_counts" output to dataframe

  21. 21

    Change values in pandas dataframe according to value_counts()

  22. 22

    Pandas reduce number of categorical variables in value_counts() tabulation

  23. 23

    pandas sparse data frame value_counts not working

  24. 24

    pandas value_counts() with multiple values in list form?

  25. 25

    Not getting 0 index from pandas value_counts()

  26. 26

    Group by and find top n value_counts pandas

  27. 27

    Leading or trailing whitespace and pandas value_counts vs boolean selection

  28. 28

    pandas value_counts with bins applied to a groupby produces incorrect results

  29. 29

    Python Pandas: Convert ".value_counts" output to dataframe

HotTag

Archive