【问题标题】:Name of one specific column in Pandas is not changingPandas 中一个特定列的名称没有改变
【发布时间】:2020-08-17 16:50:46
【问题描述】:

我正在尝试更改数据框中几列的名称。下面的代码能够更改所有列的名称,除了一个。行为不端的列 ('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')]

【问题讨论】:

  • print (df.columns.tolist()) 是什么?然后可以检查有问题的列名
  • 请显示df.columns的输出。这看起来很奇怪。
  • 当我们遇到奇怪的字符串时,只需检查代码:print([(i, hex(ord(i))) for i in df.columns[8]])
  • @jezrael,是的,列名似乎很奇怪。可以改正吗?
  • Tot Cases/1M pop 更改为'Tot\xa0Cases/1M pop'

标签: python pandas


【解决方案1】:

print (df.columns.tolist())测试后可以查看this\xa0的值是多少:

\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'})

【讨论】:

【解决方案2】:

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

df.columns.values[8] = "New name"

【讨论】:

  • 不要,检查this
  • 它可能有效,但也可能无效,所以我建议避免它。
猜你喜欢
  • 2015-04-15
  • 1970-01-01
  • 2022-11-28
  • 2014-01-19
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
相关资源
最近更新 更多