Python 替换丢失的值&;使用Numpy和Pandas更新数据帧中的旧值

Python 替换丢失的值&;使用Numpy和Pandas更新数据帧中的旧值,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,我试图用np.nan值替换数据帧中由“…”反映的缺失值。 我还想更新一些旧值,但我的方法似乎不起作用 这是我的密码: import numpy as np import pandas as pd def func(): energy=pd.ExcelFile('Energy Indicators.xls').parse('Energy') energy=energy.iloc[16:][['Environmental Indicators: Energy','Unname

我试图用
np.nan
值替换数据帧中由“…”反映的缺失值。 我还想更新一些旧值,但我的方法似乎不起作用

这是我的密码:

import numpy as np 
import pandas as pd 


def func():
    energy=pd.ExcelFile('Energy Indicators.xls').parse('Energy')
    energy=energy.iloc[16:][['Environmental Indicators: Energy','Unnamed: 3','Unnamed: 4','Unnamed: 5']].copy()
    energy.columns=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']
    o="..."
    n=np.NaN

    # Trying to replace missing values with np.nan values 
    energy[energy['Energy Supply']==o]=n


    energy['Energy Supply']=energy['Energy Supply']*1000000


    # Here, I want to replace old values by new ones ==> Same problem 
    old=["Republic of Korea","United States of America","United Kingdom of " 
                                +"Great Britain and Northern Ireland","China, Hong "
                                +"Kong Special Administrative Region"]
    new=["South Korea","United States","United Kingdom","Hong Kong"]
    for i in range(0,4):


        energy[energy['Country']==old[i],'Country']=new[i]


    return energy

下面是我正在处理的
.xls
文件:

我将使用基于regex的
df来执行此操作

energy = energy.replace(r'\s*\.+\s*', np.nan, regex=True)

MaxU提出了一个新的方法,如果你的单元格中除了点之外没有任何特殊的/空白字符,那么这个方法就可以工作

energy = energy.replace('...', np.nan, regex=False)

我认为应该是
energy=energy.replace(“…”,np.nan,regex=False)
@MaxU regex默认为False,这意味着列值(可能是前导空格)有问题,所以我决定使用regex。也将添加您的<代码>能量=能量。替换(“…”,np.nan)
效果良好