[英]Data frame columns wont convert to float data type in python
I'm trying to get a percent change between two columns in a pandas dataframe that was built from an sqlite database, however when I try to do the calculations I cant because the columns are not of the float type.我试图在 pandas dataframe 中获得两列之间的百分比变化,它是从 sqlite 数据库构建的,但是当我尝试进行浮点类型的计算时,我不能。 i have tried most ways of converting them like the
.astype
, to_numeric
I've also tried to wrap it in float()
, but none of this seems to work the data remains as a object type and I can do any computations on it.我已经尝试了大多数转换它们的方法,比如
.astype
, to_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)
have check the Type of data2.检查数据类型2。 and it seems like you are passing str as dtype argument in pd.Dataframe, have you tried to change into float?
并且您似乎在 pd.Dataframe 中将 str 作为 dtype 参数传递,您是否尝试过更改为浮点数? Please let me see the picture exactly and your output result, might be I am not understanding correctly.
请让我看清楚图片和您的 output 结果,可能是我理解不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.