簡體   English   中英

計算 csv 文件中出現的次數

[英]count number of occurrences in csv file

我希望能夠制作一個包含某個房間出現次數的條形圖(存儲在 csv 文件中)。 房間的數量在開始時沒有定義。

這是存儲在 csv 文件中的數據:

csv數據

這是我想要顯示的圖表類型,但使用我目前擁有的代碼,它不顯示出現次數。

圖形

我能做些什么來解決這個問題?

到目前為止,這是我的代碼。

with open('calls.csv') as File:  
    plots = csv.reader(File, delimiter = ',')

   for row in plots:
        if (row[0] not in x):
            x.append(row[0])
       numberOfRooms = len(x)

    for i in range(numberOfRooms):
        occurence = 0
        for row in plots:
            if(x[i] == row[0]):
                occurence += 1
        y.append(occurence)

plt.bar(x,y, width = 0.7, label="Number of calls")
plt.xlabel('room')
plt.ylabel('number of calls')
plt.title('Number of calls per room')
plt.legend()
plt.show()

由於您標記了 Pandas,您可以執行以下操作:

# read dataframe from csv
df = pd.read_csv('calls.csv')

# count the values in the first column
counts = df.iloc[:,0].value_counts()

# plot the count
counts.plot.bar()

也就是說,您當然可以使用csv package,但您可能應該使用不同的數據結構,例如字典:

with open('calls.csv') as File:  
    data = csv.reader(File, delimiter = ',')

    counts = {}
    
    # loop through data
    for row in data:
        room = row[0]

        # initiate if room is first seen
        if room not in counts: counts[room] = 0

        # increase the count
        counts[room] += 1
    
# plot the counts
plt.bar(list(counts.keys()), list(counts.values()) )

對於這樣的任務,僅使用“內置”,我會使用字典來積累數據

data = {}
with open('calls.csv') as File:  
    plots = csv.reader(File, delimiter = ',')
    for row in plots:
        # increments the counter of occurrences, 
        # if the key is not already in there, .get() will return the 0 as a default
        data[row[0]] = data.get(row[0],0) + 1
# at this point we have processed the whole file, so let's prepare data for plotting
# we take the keys of the dictionary and sort them for the x axis
x = list(data.keys())
x.sort()
# the we take the values, the count of occurrences, in the same order
y = [data[i] for i in x]
# then just ... plot it. 
plt.bar(x, y, width = 0.7, label="Number of calls")
plt.xlabel('room')
plt.ylabel('number of calls')
plt.title('Number of calls per room')
plt.legend()
plt.show()

如果沒有pandas ,您可以將每個房間的呼叫次數保存在dict中,並將其用於 plot:

import csv
import matplotlib.pyplot as plt

rooms = dict()
with open("calls.csv") as infile:
    csvreader = csv.reader(infile)
    
    for row in csvreader:
        if row[0] not in rooms:
            rooms[row[0]] = 0
        rooms[row[0]] += 1

plt.bar(rooms.keys(), rooms.values(), width=0.7)
plt.xlabel("room")
plt.ylabel("number of calls")
plt.title("Number of calls per room")
Output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM