Home>

I implemented a function to count the number of data with reference to the Internet.

df = pd.DataFrame ({'class': ['a', 'a', 'a', 'b', 'b', 'c'], 'score': [0,20,10,1,3,21]})
print (df)
# class score
# 0 a 0
# 1 a 20
# 2 a 10
# 3 b 1
# 4 b 3
# 5 c 21
print (df.groupby ('class'). count ())
# score
#class
#a 3
#b 2
#c 1


Implementation was successful like this.
However, I wanted to extract the number of data and tried it variously, but it was not successful.

  • Answer # 1

    You can get the number of rows, the number of columns and the number of data in the data frame as follows.

    import pandas as pd
    df = pd.DataFrame ({'class': ['a', 'a', 'a', 'b', 'b', 'c'], 'score': [0,20,10,1, 3,21]})
    print (len (df)) # number of columns
    print (len (df.index)) # number of lines
    print (df.size) # number of data

    Is it the number of unique data types?

    import pandas as pd
    df = pd.DataFrame ({'class': ['a', 'a', 'a', 'b', 'b', 'c'], 'score': [0,20,10,1, 3,21]})
    print (df ["class"]. value_counts ())
    # a 3
    # b 2
    # c 1
    # Name: class, dtype: int64