如何在Python中相乘两个对象数据类型?
App object
Category object
Rating float64
Reviews int64
Size_MBs float64
Installs object
Type object
Price object
Content_Rating object
Genres object
dtype: object
import pandas as pd
df=pd.read_csv('apps.csv')
clean_df=df.dropna()
clean_df.loc[:, "Installs"] = clean_df.loc[:, "Installs"].astype(str).str.replace(",", "")
clean_df.loc[:, "Installs"] = pd.to_numeric(clean_df.loc[:, "Installs"])
clean_df.loc[:,"Price"] = clean_df.loc[:,"Price"].astype(str).str.replace('$', "")
clean_df.loc[:, "Price"] = pd.to_numeric(clean_df.loc[:,"Price"])
#Line 8:
clean_df['Revenue_Estimate']= clean_df.Installs * clean_df.Price
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation:
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
clean_df['Revenue_Estimate']= clean_df.Installs * clean_df.Price
df_apps_clean['Revenue_Estimate'] = df_apps_clean.Installs.mul(df_apps_clean.Price)
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
clean_df['Revenue_Estimate'] = clean_df.Installs.mul(clean_df.Price)
这是来自Udemy培训课程的内容(第76天)。
我刚开始学习Python,所以如果我提问的方式不太对,请多包涵。任何帮助都非常感谢。谢谢!我有一个Excel表格,里面有好几列,每一列的数据类型如下所示。第8行在Pycharm中给我报错,说我不能进行乘法运算。我的Excel表格是:apps.csv,里面有10列。请看附上的图片。
apps.csv我想把Excel表格中的“价格”和“安装”这两列相乘,然后添加一个新列,命名为“收入估算”。
错误信息:
=============
我也试过这个,但没有成功。
错误信息:
2 个回答
-1
import pandas as pd
df = pd.read_csv('apps.csv')
clean_df = df.dropna()
clean_df['Installs'] = clean_df['Installs'].str.replace(',', '').astype(int)
clean_df['Price'] = clean_df['Price'].str.replace('$', '').astype(float)
clean_df['Revenue_Estimate'] = clean_df['Installs'] * clean_df['Price']
试着把安装数量转换成整数,把价格转换成浮点数,这样可能会对你有帮助。
如果这样还是不行,请再提供一些详细信息。
0
把第8行改成:
clean_df.insert(10, "Revenue Estimate", clean_df.loc[:, "Price"] * clean_df.loc[:, "Installs"], True)
这里的10是指我想在“收入估算”这个名字下面添加的那一列。