簡體   English   中英

數據框列不會轉換為 python 中的浮點數據類型

[英]Data frame columns wont convert to float data type in python

我試圖在 pandas dataframe 中獲得兩列之間的百分比變化,它是從 sqlite 數據庫構建的,但是當我嘗試進行浮點類型的計算時,我不能。 我已經嘗試了大多數轉換它們的方法,比如.astypeto_numeric我也嘗試將它包裝在float()中,但是這些似乎都不起作用,數據仍然是 object 類型,我可以對其進行任何計算。

conn = sqlite3.Connection("PriceData.db")
c = conn.cursor()
c.execute('SELECT productId, edition, lowPrice, midPrice, highPrice, date FROM PriceData WHERE date = "2020-07-30"')
data = c.fetchall()
columns = ['productId','edition', 'lowPrice','midPrice', 'highPrice', 'date']
dfPrev = pd.DataFrame(data, columns=columns, dtype = str)
conn = sqlite3.Connection("PriceData.db")
c = conn.cursor()
c.execute('SELECT productId, edition, lowPrice, midPrice, highPrice, date FROM PriceData WHERE date = "2020-07-31"')
data2 = c.fetchall()
dfToday = pd.DataFrame(data2, columns=columns, dtype = str)

result = pd.merge(dfPrev,dfToday, on = ['productId', 'edition'])

result['lowPercentChange'] = result[['lowPrice_x', 'lowPrice_y']].apply(lambda x :percentChange(result['lowPrice_x'], result['lowPrice_y']), axis = 1)
print(result)

print(result.dtypes)

檢查數據類型2。 並且您似乎在 pd.Dataframe 中將 str 作為 dtype 參數傳遞,您是否嘗試過更改為浮點數? 請讓我看清楚圖片和您的 output 結果,可能是我理解不正確。

暫無
暫無

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

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