如何从Pandas的value | 您所在的位置:网站首页 › 从诗句中提取出来的名字 › 如何从Pandas的value |
如何从Pandas的value_counts()中提取数值名称和计数
在这篇文章中,我们将学习如何使用panda的values_count()来提取名字和值。panda库中配备了许多有用的函数,’value_counts’就是其中之一。这个函数返回pandas数据框中唯一项目的计数。 语法: .value_count() 方法: 导入所需模块 制作数据框架 Process using value_count() Display data例子1:要打印所有唯一的国家和列表中的第一个国家名称。 tolist()函数返回一个值的列表。 语法: Index.tolist() 参数 : None 返回: list import pandas as pd import matplotlib.pyplot as plt # Make example dataframe df = pd.DataFrame([(1, 'Germany'), (2, 'France'), (3, 'Indonesia'), (4, 'France'), (5, 'France'), (6, 'Germany'), (7, 'UK'), (8, 'India'), (9, 'India'), (10, 'Germany') ], columns=['groupid', 'country'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']) # print all unique country name in the list su1 = df['country'].value_counts().index.tolist() print(su1) # print 1st unique country name present in a list su2 = df['country'].value_counts().index.tolist()[0] print(su2)输出: 示例2:要打印该列的所有唯一值和该列的第一个值。 value_count()计算一个列中数值的唯一出现次数 语法 : Index.value_count() 参数: 无 返回:该列中每个唯一值出现的次数。 import pandas as pd import matplotlib.pyplot as plt # Make example dataframe df = pd.DataFrame([(1, 'Germany'), (2, 'France'), (3, 'Indonesia'), (4, 'France'), (5, 'France'), (6, 'Germany'), (7, 'UK'), (8, 'India'), (9, 'India'), (10, 'Germany') ], columns=['groupid', 'country'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']) # print country name and counts su3 = df['country'].value_counts() print(su3) # print 1st country count in a list su4 = df['country'].value_counts()[0] print(su4)输出: 例子3:使用一个循环从一个列表中打印我们的数据。 import pandas as pd import matplotlib.pyplot as plt # Make example dataframe df = pd.DataFrame([(1, 'Germany'), (2, 'France'), (3, 'Indonesia'), (4, 'France'), (5, 'France'), (6, 'Germany'), (7, 'UK'), (8, 'India'), (9, 'India'), (10, 'Germany') ], columns=['groupid', 'country'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']) # printing names and count using loop. for idx, name in enumerate(df['country'].value_counts().index.tolist()): print('Name :', name) print('Counts :', df['country'].value_counts()[idx])输出: 例子4:以条形图的形式打印我们的数据。 语法: matplotlib.pyplot.plot(kind=' ') 参数: kind: 图形的类型,即直线、条形。 返回:返回一个图形。 import pandas as pd import matplotlib.pyplot as plt # Make example dataframe df = pd.DataFrame([(1, 'Germany'), (2, 'France'), (3, 'Indonesia'), (4, 'France'), (5, 'France'), (6, 'Germany'), (7, 'UK'), (8, 'India'), (9, 'India'), (10, 'Germany') ], columns=['groupid', 'country'], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']) # Display data in a form of Graph df['country'].value_counts().plot(kind='bar') plt.show()输出: |
CopyRight 2018-2019 实验室设备网 版权所有 |