第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

熊貓:在數(shù)據(jù)框中重命名“未命名:*”或“ NaN”

熊貓:在數(shù)據(jù)框中重命名“未命名:*”或“ NaN”

暮色呼如 2021-03-31 21:14:17
到目前為止,這是我的代碼:import numpy as npimport pandas as pddf = pd.read_excel(r'file.xlsx', index_col=0)看起來是這樣的:我想將“未命名:*”列重命名為最后一個有效名稱。這是我嘗試過的結(jié)果: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貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個贊

您遇到的問題與列和索引是pd.Index對象這一事實(shí)有關(guān)。pandas Index的fillna方法采用的參數(shù)與pandas Series或DataFrame的fillna方法采用的參數(shù)不同。我在下面做了一個玩具示例:


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

您原始的正則表達(dá)式無法捕獲整個列名,我們來解決這個問題。


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

現(xiàn)在,讓我們將索引轉(zhuǎn)換為一系列,以便我們可以使用和的一個正則表達(dá)式的.replace和.fillna方法,pd.Series將相關(guān)的列名替換為ffill。最后,我們將其轉(zhuǎn)換為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


查看完整回答
反對 回復(fù) 2021-04-09
  • 3 回答
  • 0 關(guān)注
  • 360 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號