Assignment / modification of values in an indexed subframe in Pandas

拟墨画扇 提交于 2020-01-04 01:56:49

问题


The documentation for indexing has few examples of assignment of a non-scalar right hand side.

In this case, I want to modify a subset of my dataframe, and what I did prior to v.13 no longer works.

import pandas as pd
from numpy import *

data = {'me':list('rttti'),'foo': list('aaade'), 'bar': arange(5)*1.34+2, 'bar2': arange(5)*-.34+2}
df = pd.DataFrame(data).set_index('me')

print df

df.loc['r',['bar','bar2']]*=2.0
print df
df.loc['t','bar']*=2.5
# Above fails: ValueError: Must have equal len keys and valuewhen setting with an iterable
print df
df.loc['t',['bar','bar2']]*=2.0
# Above fails: *** InvalidIndexError: Reindexing only valid with uniquely valued Index objects
print df

I want to be able to assign a matrix of values to a matrix of locations using loc or, in this simplifed case, I just want to modify them by some simple operation like multiplication.

The first modification works (the index value is unique), and the others do not, though they give different errors.

来源:https://stackoverflow.com/questions/22178642/assignment-modification-of-values-in-an-indexed-subframe-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!