我在 MongoDB 中有一個資料庫,顯示了 2019 年的太陽能生產情況。我將它匯入到一個帶有 json 檔案的集合中。我現在必須使用 plot() 函式在 python 中的圖表上顯示這些資料。我從我的 MongoDB 中讀出資料并將其放入 pandas DataFrame。
問題是我收到一個 TypeError 說“沒有要繪制的數字資料”。我認為這是因為我的資料框中的資料仍然是文本形式,而不是實際資料。所以我需要以某種方式將其轉換為數字資料和日期時間。
這是我當前的代碼:
import pandas as pd
import matplotlib.pyplot as plt
import pymongo
my_client = pymongo.MongoClient("mongodb://localhost:27017/")
my_db = my_client["ExamenVoorbeeld"]
my_collection = my_db["Opbrengst_zonnepanelen"]
lst_power_production = list(my_collection.find(filter={}, projection={"_id": 0, "Production": 1, "DateTime": 1}))
df_mongo = pd.DataFrame(lst_power_production)
print(df_mongo)
df_mongo.plot(x="DateTime", y = "Production", xlabel="date", ylabel="production (kWh)", figsize=(20,10), title="Solar energy production in 2019")
附加了我的資料庫的額外螢屏截圖。 資料庫
uj5u.com熱心網友回復:
您可以嘗試更改資料框列的資料型別,例如:
df_mongo['Production'] = df_mongo['Production'].astype(int)
df_mongo['DateTime'] = pd.to_datetime(df_mongo['DateTime'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/410192.html
標籤:
上一篇:檢查bson欄位的`type`以確定它是否是$numberDecimal(mongo/node)
下一篇:如何限制從同一服務器中的其他dockerized應用程式到dockerizedMongoDB的連接并拒絕對27017的公共訪問
