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

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

pandas:將一個矩陣轉(zhuǎn)換為另一個

pandas:將一個矩陣轉(zhuǎn)換為另一個

紫衣仙女 2022-06-02 11:06:40
我正在嘗試轉(zhuǎn)換一個矩陣(df),所以我得到另一個(df2),并且不知道如何......[df]      X     Y     Za  None     x  Noneb  None  None     xc     x  None  None[df2]    X     Y     Z None     a  None None  None     b    c  None  None如果有幫助,這里是 df 的構(gòu)造:import pandas as pddf = pd.DataFrame([[None, 'x', None], [None, None, 'x'], ['x', None, None]],     index=['a', 'b', 'c'],     columns=['X', 'Y', 'Z'])print(f'\n{df.to_string()}')
查看完整描述

3 回答

?
至尊寶的傳說

TA貢獻1789條經(jīng)驗 獲得超10個贊

您可以使用mask:


df = df.mask(df.notnull(), df.index)


print(df)

      X     Y     Z

a  None     a  None

b  None  None     b

c     c  None  None


查看完整回答
反對 回復(fù) 2022-06-02
?
www說

TA貢獻1775條經(jīng)驗 獲得超8個贊

你可以檢查mul


df.eq('x').mul(df.index.tolist(),0)

Out[109]: 

   X  Y  Z

a     a   

b        b

c  c      


查看完整回答
反對 回復(fù) 2022-06-02
?
忽然笑

TA貢獻1806條經(jīng)驗 獲得超5個贊

另一種使用方法apply:


ddf = df.apply(lambda row : pd.Series(row.name if el == 'x' else el for el in row), axis=1)

ddf.columns = df.columns

最后一行因為列名丟失了,所以你需要重新評估它們。

基本上是用'x'行索引替換所有值,保留其他值(即使它們不是None)。


結(jié)果ddf是:


      X     Y     Z

a  None     a  None

b  None  None     b

c     c  None  None


查看完整回答
反對 回復(fù) 2022-06-02
  • 3 回答
  • 0 關(guān)注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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