熊猫中某一特定列的名称未更改

时间:2020-05-03 09:31:06

标签: python pandas

我正在尝试更改数据框中几列的名称。下面的代码能够更改除一列之外的所有列的名称。行为异常的列('Tot Cases/1M pop')的名称前后没有空格。我无法找出问题所在。欣赏建议。

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 216 entries, 0 to 215
Data columns (total 12 columns):
Country,Other       216 non-null object
TotalCases          216 non-null int64
NewCases            139 non-null object
TotalDeaths         178 non-null float64
NewDeaths           91 non-null object
TotalRecovered      207 non-null float64
ActiveCases         216 non-null int64
Serious,Critical    137 non-null float64
Tot Cases/1M pop    214 non-null float64
Deaths/1M pop       176 non-null float64
TotalTests          179 non-null float64
Tests/ 1M pop       179 non-null float64
dtypes: float64(7), int64(2), object(3)
memory usage: 20.4+ KB

df = df.rename(columns={'Country,Other': 'Country_or_Other','Serious,Critical': 'Serious_or_Critical','Tot Cases/1M pop':'Cases_1M_pop', 'Deaths/1M pop':'Deaths_per_1M_pop','Tests/ 1M pop':'Tests_per_1M_pop'})

df.head(3)
    Country_or_Other    TotalCases  NewCases    TotalDeaths     NewDeaths   TotalRecovered  ActiveCases     Serious_or_Critical     Tot Cases/1M pop    Deaths_per_1M_pop   TotalTests  Tests_per_1M_pop
0   World   3481349     83255.0     244663.0    5215.0  1120908.0   2115778     50860.0     447.0   31.4    NaN     NaN
1   China   82875   1.0     4633.0  NaN     77685.0     557     37.0    58.0    3.0     NaN     NaN
2   USA     1160774     29744.0     67444.0     1691.0  173318.0    920012  16475.0     3507.0  204.0   6931132.0   20940.0

for col in df.columns:
    print(col, len(col))
Country_or_Other 16
TotalCases 10
NewCases 8
TotalDeaths 11
NewDeaths 9
TotalRecovered 14
ActiveCases 11
Serious_or_Critical 19
Tot Cases/1M pop 16
Deaths_per_1M_pop 17
TotalTests 10
Tests_per_1M_pop 16

print (df.columns.tolist()) 
['Country_or_Other',
 'TotalCases',
 'NewCases',
 'TotalDeaths',
 'NewDeaths',
 'TotalRecovered',
 'ActiveCases',
 'Serious_or_Critical',
 'Tot\xa0Cases/1M pop',
 'Deaths_per_1M_pop',
 'TotalTests',
 'Tests_per_1M_pop']

print([(i, hex(ord(i))) for i in df.columns[8]])
    [('T', '0x54'), ('o', '0x6f'), ('t', '0x74'), ('\xa0', '0xa0'), ('C', '0x43'), ('a', '0x61'), ('s', '0x73'), ('e', '0x65'), ('s', '0x73'), ('/', '0x2f'), ('1', '0x31'), ('M', '0x4d'), (' ', '0x20'), ('p', '0x70'), ('o', '0x6f'), ('p', '0x70')]

2 个答案:

答案 0 :(得分:3)

通过\xa0测试后,您可以检查thisprint (df.columns.tolist())的值是什么:

\ xa0实际上是Latin1(ISO 8859-1)和chr(160)中的不间断空格。您应该将其替换为空格。

因此更改有问题的列名称,例如:

df = df.rename(columns={'Country,Other': 'Country_or_Other',
                       'Serious,Critical': 'Serious_or_Critical',
                       'Tot\xa0Cases/1M pop':'Cases_1M_pop',
                       'Deaths/1M pop':'Deaths_per_1M_pop',
                       'Tests/ 1M pop':'Tests_per_1M_pop'})

答案 1 :(得分:1)

您还可以通过直接寻址索引来重命名特定的列,如下所示:

function check() {
  var Name=document.getElementById("Name");
  var Surname=document.getElementById("Surname");
  if (Name==null && Name="" || Surname==null && Surname="") {
    alert("Enter the name or surname");
    return false;
  }
  return true;  
}
相关问题