[英]Counting the number of occurrences of text in a pandas dataframe
我受命使用熊貓檢查csv文件中出現的文本的出現次數。 但是我對熊貓圖書館很困惑,如果有人可以幫助我,我將不勝感激。
//例
//title row [ round 1, round 2, round 3]
//row 1 [ 1, 2, 0]
//row 2 [ 2, 2, 0]
//row 3 [ 0, 1, 1]
所以我需要輸出
出現次數:
0 = 3
1 = 2
2 = 3
知道我該怎么做嗎?
value_counts
在系列中計算此結果。 您首先需要通過將所有列堆疊為一個來獲得該系列:
df.stack()
Out[14]:
0 round1 1
round2 2
round3 0
1 round1 2
round2 2
round3 0
2 round1 0
round2 1
round3 1
dtype: int64
df.stack().value_counts()
Out[15]:
2 3
1 3
0 3
dtype: int64
使用pandas.read_csv導入.csv文件。 然后,您可以使用pandas.value_counts遍歷列
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.