gpt4 book ai didi

python - 替换 pandas 多索引数据框中的值

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:09 25 4
gpt4 key购买 nike

我想更改 pandas 数据框中的值,并且我认为我误解了索引的工作原理。

将 pandas 导入为 pd
idx = pd.IndexSlice
df.loc[idx[(0, 2006.0, '01019_13055_01073_01009_01055')],idx[('moment_25','P517')]]

我得到了输出

Out[376]: 
moment_25 P517 0.665873
Name: (0, 2006.0, 01019_13055_01073_01009_01055), dtype: float64

我想将 df 中的值 0.665873 更改为 1。我已经尝试过

df.ix[idx[(0, 2006.0,'01019_13055_01073_01009_01055')],idx[('moment_25','P517')]]=1

但我收到错误

Exception: cannot handle a non-unique multi-index!

我尝试使用示例数据框复制该问题,但无济于事。

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
df.loc[idx['A'],idx[('baz','one')]]

Out[391]: -0.17935592549360641

我认为问题在于,当我使用实际数据时,我得到了一系列输出,但当我使用练习数据时,我得到了一个 float 。为什么我得到的是该系列而不是 float 0.665873?

最佳答案

使用set_value更改数据帧中的值。下面的例子:

import pandas as pd
import numpy as np
dfp = pd.DataFrame({'A' : [np.NaN,np.NaN,3,4,5,5,3,1,5,np.NaN],
'B' : [1,0,3,5,0,0,np.NaN,9,0,0],
'C' : ['Pharmacy of IDAHO','Access medicare arkansas','NJ Pharmacy','Idaho Rx','CA Herbals','Florida Pharma','AK RX','Ohio Drugs','PA Rx','USA Pharma'],
'D' : [123456,123456,1234567,12345678,12345,12345,12345678,123456789,1234567,np.NaN],
'E' : ['Assign','Unassign','Assign','Ugly','Appreciate','Undo','Assign','Unicycle','Assign','Unicorn',]})
print(dfp)


A B C D E
0 NaN 1.0 Pharmacy of IDAHO 123456.0 Assign
1 NaN 0.0 Access medicare arkansas 123456.0 Unassign
2 3.0 3.0 NJ Pharmacy 1234567.0 Assign
3 4.0 5.0 Idaho Rx 12345678.0 Ugly
4 5.0 0.0 CA Herbals 12345.0 Appreciate
5 5.0 0.0 Florida Pharma 12345.0 Undo
6 3.0 NaN AK RX 12345678.0 Assign
7 1.0 9.0 Ohio Drugs 123456789.0 Unicycle
8 5.0 0.0 PA Rx 1234567.0 Assign
9 NaN 0.0 USA Pharma NaN Unicorn
# ^^Check HEERE^^

更改和输出:

dfp.set_value(9, 'C', 10)
print(dfp)

A B C D E
0 NaN 1.0 Pharmacy of IDAHO 123456.0 Assign
1 NaN 0.0 Access medicare arkansas 123456.0 Unassign
2 3.0 3.0 NJ Pharmacy 1234567.0 Assign
3 4.0 5.0 Idaho Rx 12345678.0 Ugly
4 5.0 0.0 CA Herbals 12345.0 Appreciate
5 5.0 0.0 Florida Pharma 12345.0 Undo
6 3.0 NaN AK RX 12345678.0 Assign
7 1.0 9.0 Ohio Drugs 123456789.0 Unicycle
8 5.0 0.0 PA Rx 1234567.0 Assign
9 NaN 0.0 10 NaN Unicorn
# ^^The CHANGE^^

如果您具体询问有关索引的问题,请检查 here

使用上面链接的方法:

dfp.ix[0, 'C'] = 'x'
# vv Check Below vv
A B C D E
0 NaN 1.0 x 123456.0 Assign
1 NaN 0.0 Access medicare arkansas 123456.0 Unassign
2 3.0 3.0 NJ Pharmacy 1234567.0 Assign
3 4.0 5.0 Idaho Rx 12345678.0 Ugly
4 5.0 0.0 CA Herbals 12345.0 Appreciate
5 5.0 0.0 Florida Pharma 12345.0 Undo
6 3.0 NaN AK RX 12345678.0 Assign
7 1.0 9.0 Ohio Drugs 123456789.0 Unicycle
8 5.0 0.0 PA Rx 1234567.0 Assign
9 NaN 0.0 10 NaN Unicorn

关于python - 替换 pandas 多索引数据框中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com