亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何將 masked pandas DataFrame 列設置為列表列

如何將 masked pandas DataFrame 列設置為列表列

月關寶盒 2023-07-27 09:58:01
如何將列中的某些單元格設置為列表列表中的列表,其中列表列表的長度與單元格的數量相同?運行我嘗試的部分時,出現以下錯誤:ValueError:使用 ndarray 設置時必須具有相等的 len 鍵和值我想要的 DataFrame,如下明確定義,desired如下所示:   include   array0     True  [1, 2]1    False     NaN2    False     NaN3     True  [3, 4]4    False     NaN嘗試過的代碼:import pandas as pd# This is what I trieda = pd.DataFrame({'include': [True, False, False, True, False]})a.loc[a['include'], 'array'] = [[1, 2], [3, 4]]# This is what I wantdesired = pd.DataFrame({'include': [True, False, False, True, False],                        'array': [[1, 2], np.nan, np.nan, [3, 4], np.nan]})
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

不太確定你想做什么,但你可以將其轉換為 apandas.Series并對齊索引:


a.loc[a['include'], 'array'] = pd.Series([[1, 2], [3, 4]], index=[0, 3])


print(a)

   include   array

0     True  [1, 2]

1    False     NaN

2    False     NaN

3     True  [3, 4]

4    False     NaN

要保持索引的分配通用而不是硬編碼,請使用:


a.loc[a['include'], 'array'] = pd.Series([[1, 2], [3, 4]], a[a['include']].index)


查看完整回答
反對 回復 2023-07-27
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號