如何从 Pandas 中的 value_counts() 中提取值名称和计数?
先决条件:
- 熊猫
- matplotlib
在本文中,我们将学习如何使用 values_count() 从 panda 中提取名称和值。 panda 库配备了许多有用的函数,'value_counts' 就是其中之一。此函数返回熊猫数据框中唯一项目的计数。
句法:
方法:
- 导入所需的模块。
- 制作数据框
- 使用 value_count() 处理
- 显示数据
示例 1:打印列表中所有唯一的国家和第一个国家名称。
tolist() function return a list of the values.
Syntax: Index.tolist()
Parameters : None
Returns : list
Python3
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)
Python3
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)
Python3
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])
Python3
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()
输出:
示例 2:打印列的所有唯一值和列的第一个值。
value_count() counts Unique Occurrences of Values in a Column
Syntax: Index.value_count()
Parameters: None
Returns: the count of occurrences of each of the unique values in this column.
蟒蛇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'])
# 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:使用列表中的循环打印我们的数据。
蟒蛇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:以条形图的形式打印我们的数据。
Syntax: matplotlib.pyplot.plot(kind=’ ‘)
Parameters: kind: type of graph, i.e. line, bar.
Returns: This returns a Graph.
蟒蛇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'])
# Display data in a form of Graph
df['country'].value_counts().plot(kind='bar')
plt.show()
输出: