"Pandas的valuecounts函数排序" 修訂間的差異
出自 YTYZX有图有真相的百科
(创建页面,内容为“ Python的pandas中,可以使用value_counts函数统计返回一个包含计数的Series,默认以值排序输出,可以在后面加上sort_index(),从而按...”) |
|||
(未顯示同一使用者於中間所作的 2 次修訂) | |||
行 16: | 行 16: | ||
2.输入print(ITDevices_record['Quantity'].value_counts()),以值进行排序。 | 2.输入print(ITDevices_record['Quantity'].value_counts()),以值进行排序。 | ||
+ | Quantity等于50的计数为3,所以排在第一位。 | ||
[[File:PandasValueCounts2.png]] | [[File:PandasValueCounts2.png]] | ||
3.输入print(ITDevices_record['Quantity'].value_counts(sort=False).sort_index()),以索引进行排序。 | 3.输入print(ITDevices_record['Quantity'].value_counts(sort=False).sort_index()),以索引进行排序。 | ||
+ | 按照Quantity的大小(Quantity为30的排在第一位)进行排序。 | ||
[[File:PandasValueCounts3.png]] | [[File:PandasValueCounts3.png]] |
於 2022年8月26日 (五) 17:48 的最新修訂
Python的pandas中,可以使用value_counts函数统计返回一个包含计数的Series,默认以值排序输出,可以在后面加上sort_index(),从而按照索引index输出。 1. 输入一下内容生成一个DataFrame. import pandas as pd ITDevices_record = pd.DataFrame({'Quantity': {0: 50, 1: 50, 2: 70, 3: 50, 4: 80, 5: 100, 6: 30, 7: 106}, 'Product_Names': {0: 'Mosuse', 1: 'Keyboard', 2: 'Headphones', 3: 'CPU', 4: 'Flash Drives', 5: 'Tablets', 6: 'Android Box', 7: 'LCD'}, 'Product_Prices': {0: 100, 1: 70, 2: 150, 3: 1500, 4: 70, 5: 1700, 6: 2500, 7: 1600},}) print(ITDevices_record)
2.输入print(ITDevices_record['Quantity'].value_counts()),以值进行排序。 Quantity等于50的计数为3,所以排在第一位。
3.输入print(ITDevices_record['Quantity'].value_counts(sort=False).sort_index()),以索引进行排序。 按照Quantity的大小(Quantity为30的排在第一位)进行排序。