为了账号安全,请及时绑定邮箱和手机立即绑定

熊猫:在数据框中重命名“未命名:*”或“ NaN”

熊猫:在数据框中重命名“未命名:*”或“ NaN”

暮色呼如 2021-03-31 21:14:17
到目前为止,这是我的代码:import numpy as npimport pandas as pddf = pd.read_excel(r'file.xlsx', index_col=0)看起来是这样的:我想将“未命名:*”列重命名为最后一个有效名称。这是我尝试过的结果:df.columns = df.columns.str.replace('Unnamed.*', method='ffill')---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-253-c868b8bff7c7> in <module>()----> 1 df.columns = df.columns.str.replace('Unnamed.*', method='ffill')TypeError: replace() got an unexpected keyword argument 'method'如果我这样做,这是“有效的”df.columns = df.columns.str.replace('Unnamed.*', '')但是我有空白值或NaN(如果我将'替换为'NaN'。然后我尝试:df.columns = df.columns.fillna('ffill')哪个没有效果。所以我尝试了inplace = True:df.columns = df.columns.fillna('ffill',inplace = True)---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-279-cce486472d5b> in <module>()----> 1 df.columns = df.columns.fillna('ffill', inplace=True)TypeError: fillna() got an unexpected keyword argument 'inplace'然后我尝试了另一种方式:i = 0while i < len(df.columns):    if df.columns[i] == 'NaN':        df.columns[i] = df.columns[i-1]    print(df.columns[i])    i += 1这给了我这个错误:Oil158 RGN MisturaAccess West Winter Blend ---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-246-bc8fa6881b1a> in <module>()      2 while i < len(df.columns):      3     if df.columns[i] == 'NaN':----> 4         df.columns[i] = df.columns[i-1]      5     print(df.columns[i])      6     i += 1~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in __setitem__(self, key, value)   2048    2049     def __setitem__(self, key, value):-> 2050         raise TypeError("Index does not support mutable operations")   2051    2052     def __getitem__(self, key):TypeError: Index does not support mutable operations
查看完整描述

3 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

您遇到的问题与列和索引是pd.Index对象这一事实有关。pandas Index的fillna方法采用的参数与pandas Series或DataFrame的fillna方法采用的参数不同。我在下面做了一个玩具示例:


import pandas as pd

import numpy as np

df = pd.DataFrame(

         {'a':[1], 'Unnamed:1':[1], 'Unnamed:2':[1], 'b':[1], 'Unnamed:3':[1]}, 

         columns=['a', 'Unnamed:3', 'Unnamed:1', 'b', 'Unnamed:2']))

df 

#   a  Unnamed:3  Unnamed:1  b  Unnamed:2

#0  1          1          1  1          1

您原始的正则表达式无法捕获整个列名,我们来解决这个问题。


df.columns.str.replace('Unnamed:*', '') 

#Index(['a', '3', '1', 'b', '2'], dtype='object')

df.columns.str.replace('Unnamed:\d+', '')

#Index(['a', '', '', 'b', ''], dtype='object')

df.columns.str.replace('Unnamed:.+', '')

#Index(['a', '', '', 'b', ''], dtype='object')

现在,让我们将索引转换为一系列,以便我们可以使用和的一个正则表达式的.replace和.fillna方法,pd.Series将相关的列名替换为ffill。最后,我们将其转换为pd.Index


pd.Index(

    pd.Series(

        df.columns

    ).replace('Unnamed:\d+', np.nan, regex=True).fillna(method='ffill')

)

#Index(['a', 'a', 'a', 'b', 'b'], dtype='object')


df.columns = pd.Index(pd.Series(df.columns).replace('Unnamed:\d+', np.nan, regex=True).fillna(method='ffill'))

df.head() 

#   a  a  a  b  b

#0  1  1  1  1  1


查看完整回答
反对 回复 2021-04-09
  • 3 回答
  • 0 关注
  • 324 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号