简体   繁体   English

数据框列不会转换为 python 中的浮点数据类型

[英]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.我已经尝试了大多数转换它们的方法,比如.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)

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.

相关问题 将字符串转换为数据框中多列的浮点数 - Convert string into float for multiple columns in data frame 无法在 Pandas 数据框中将对象数据类型转换为浮动 - Can't convert object data type to float in pandas data frame 将数据帧数据时间字符串转换为浮在python / Pandas中 - convert data frame datatime string to float in python /Pandas 在数据框上按行将浮点类型数转换为百分比 - Python - Turn a float type number into percentage by row on a Data Frame- Python 如何在 Python 中的数据框中填充类型为 Float 的列的模式值 - How to fillna with mode value of a Column with type Float in a data frame in Python 将数据框列转换为字典 - Convert data frame columns to dictionary 在数据框中的某些列上将 Float 转换为 Int - Converting Float to Int on certain columns in a data frame 如何从 API 连接中提取字节类型的 JSON 数据,并将选定的列转换为 python 数据框 - How can I extract bytes type JSON data from an API connection, and convert selected columns to a python data frame 搜索字符串类型(包含数字范围)的数据框列,用户输入为浮点类型 - Searching through data frame columns in type string (containing numeric range), with user inputs that are in type float 如何转换RAvg类型数据以在Python中正确浮动? - How to convert RAvg type data to float properly in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM