我已经将一个数据帧读入python,其中的列名包含一个欧元符号“price”。Python将此列视为price。它不允许我用欧元或欧元来引用这篇专栏文章
File "<ipython-input-53-d7f8249147e7>", line 1
df[price_€] = df[0].str.replace(r'[€,]', '').astype('float')
^
SyntaxError: invalid syntax
有什么办法把这个从列名中删除,这样我就可以开始引用它了吗?在
Tags:
不能在变量名中使用欧元符号:
您需要使用字符串:
^{pr2}$熊猫其实对我来说,欧元符号没有问题:
该文件是cp1252编码的,因此您需要指定编码:
然后改为浮动:
输出:
在字符串上使用如下lambda过滤器
输出是
^{pr2}$应使用^{} 重命名列名:
另外,
df[price_€]
是一种无效的选择列的方式,您需要传递一个字符串,这样df['price_€']
是正确的形式。在还有这个:
^{pr2}$不清楚您在这里要做什么,
df[0]
将再次引发一个KeyError
,因为要为列建立索引,您需要传递一个字符串。在我也不明白你为什么要把这个列变成一个浮点数,你还没有解释这一部分。在
相关问题 更多 >
编程相关推荐