Python 替换值并将其除以熊猫

Python 替换值并将其除以熊猫,python,pandas,Python,Pandas,我的原始数据: Main_Genre Genre_2 ... worldwide_gross year 0 Action Adventure ... $700,059,566 2018 1 Action Adventure ... $678,815,482 2018 2 Animation Action ... $608,581,744 2018 3 Action Adventure

我的原始数据:

Main_Genre Genre_2 ... worldwide_gross year 0 Action Adventure ... $700,059,566 2018 1 Action Adventure ... $678,815,482 2018 2 Animation Action ... $608,581,744 2018 3 Action Adventure ... $416,769,345 2018 4 Action Comedy ... $318,491,426 2018 当我这样做时,字符串不会转换为float

.str.replace(',', '.')
新数据:

Main_Genre imdb_rating length worldwide_gross 0 Action 7.4 135 7.000596e+25 1 Action 8.5 156 6.788155e+25 2 Animation 7.8 118 6.085817e+25 3 Action 6.2 129 4.167693e+25 4 Action 7.8 119 3.184914e+25
你的代码对我来说似乎很好。要除以百万,您可以直接除以:

数据2[全球总量]=数据2[全球总量]/1000000

您对显示格式的更改对我来说似乎很好

对于这个小例子:

作为pd进口熊猫 df=pd.数据帧{全球总量:[700059566美元,$678815482],年份:[2018年,2017年]} df[worldwide\u gross]=df[worldwide\u gross].str.replace',',.str.replace'$,.astypefloat pd.options.display.float_format='{{0:.0f}'。格式 printdf 输出为:

  worldwide_gross  year
0        700059566  2018
1        678815482  2017
你也可以试试-

df['worldwide_gross'] = df['worldwide_gross'].str.replace('\$|\,','').astype(float)
df
  Main_Genre    Genre_2  ...   worldwide_gross  year
0     Action  Adventure  ...       700059566.0  2018
1     Action  Adventure  ...       678815482.0  2018
2  Animation     Action  ...       608581744.0  2018
3     Action  Adventure  ...       416769345.0  2018
4     Action     Comedy  ...       318491426.0  2018

我的英语不好,所以我不能确切地告诉你我想做什么,我想用这种方式表达700.059.566,但不能,请澄清。你只想用逗号代替圆点?aka$318491426->318.491.426?对于这一点,你可以用。。当我把它放进去的时候,字符串并没有被转换成float,你希望它是float,但是当打印它的时候,它应该看起来像111.111.111?当真实数字是111111时?我在试着理解,但我有点难以理解你到底想不想删除+e我想说的是,在它们之间有一个点对我来说并不重要$700059566到700059566或700.059.566类似于Efyi,可以使用正则表达式组合这些替换方法。df['worldwide\u gross'].str.replace'\$\\\,',谢谢您的评论,先生。已根据您的建议更新了答案。
  worldwide_gross  year
0        700059566  2018
1        678815482  2017
df['worldwide_gross'] = df['worldwide_gross'].str.replace('\$|\,','').astype(float)
df
  Main_Genre    Genre_2  ...   worldwide_gross  year
0     Action  Adventure  ...       700059566.0  2018
1     Action  Adventure  ...       678815482.0  2018
2  Animation     Action  ...       608581744.0  2018
3     Action  Adventure  ...       416769345.0  2018
4     Action     Comedy  ...       318491426.0  2018