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

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

使用間隔索引在 DataFrame 中設置特定值

使用間隔索引在 DataFrame 中設置特定值

手掌心 2021-07-30 02:08:14
讓我有一個這樣的數據框:test_df = pd.DataFrame(        0,         index = pd.IntervalIndex.from_breaks([100, 200, 300, 400]),        columns = ['A', 'B', 'C'])所以它會給我們這個:            A   B   C(100, 200]  0   0   0(200, 300]  0   0   0(300, 400]  0   0   0現在,假設我想在給定的間隔中更改特定值,在我的數據幀中的某些間隔中具有整數值。我可以用得到的區間值,.loc通過這個weay特定定整數值: test_df.loc[250]。這會給我系列。但!第一種情況:test_df.at[250, 'B']------ValueError: At based indexing on an non-integer index can only have non-integer indexers為什么250在這種情況下我不能使用整數值?它適用于.loc.第二種情況:test_df.loc[250, 'B']------'pandas._libs.interval.IntervalTree' object has no attribute 'get_value'沒有屬性get_value?那么我怎樣才能獲得價值呢?我可以只通過系列來做,通過.loc財產獲得嗎?第三種情況:test_df.at[250, 'B'] = 10------'pandas._libs.interval.IntervalTree' object has no attribute 'set_value'主要問題我應該如何在數據框中設置特定值?
查看完整描述

3 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

.loc 如果您為它提供第二組標簽的列表,則當前有效:


test_df.loc[250, ['B']]

#B    0

#Name: (200, 300], dtype: int64


test_df.loc[250:400, ['B', 'C']]

#            B  C

#(200, 300]  0  0

#(300, 400]  0  0


test_df.loc[250, ['B']] = 10

print(test_df)

#            A   B  C

#(100, 200]  0   0  0

#(200, 300]  0  10  0

#(300, 400]  0   0  0


查看完整回答
反對 回復 2021-08-03
?
UYOU

TA貢獻1878條經驗 獲得超4個贊

請注意, 的行為loc已得到修復,并將出現在即將發布的 0.24.0 版本中:


In [1]: import pandas as pd; pd.__version__

Out[1]: '0.24.0.dev0+870.g7191af9b4'


In [2]: test_df = pd.DataFrame(

   ...:         0,

   ...:         index = pd.IntervalIndex.from_breaks([100, 200, 300, 400]),

   ...:         columns = ['A', 'B', 'C']

   ...: )


In [3]: test_df

Out[3]:

            A  B  C

(100, 200]  0  0  0

(200, 300]  0  0  0

(300, 400]  0  0  0


In [4]: test_df.loc[250, 'B']

Out[4]: 0


In [5]: test_df.loc[250, 'B'] = 100


In [6]: test_df

Out[6]:

            A    B  C

(100, 200]  0    0  0

(200, 300]  0  100  0

(300, 400]  0    0  0


查看完整回答
反對 回復 2021-08-03
?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

test_df.loc[250].at['B'] = whatever

但請確保任何與列數據類型相同的類型


查看完整回答
反對 回復 2021-08-03
  • 3 回答
  • 0 關注
  • 418 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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