我想改变我的pandas数据框中的值,我想我误解了索引的工作原理。
import pandas as 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
我认为问题在于,当我使用我的实际数据时,我得到了一个输出系列,但是当我使用练习数据时,我得到一个浮点数。为什么我得到那个系列而不是浮点数0.665873?
答案 0 :(得分:0)
使用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