[英]Obtaining a pandas series by constructing its name as a string
I'm looking to construct a series name as a string and get its values for a given index, or set its value for a particular index.我正在寻找将系列名称构造为字符串并获取给定索引的值,或为特定索引设置其值。 For example:
例如:
def getEntityValue(self, testCase, ent_order):
if ent_order == 1:
return self.testInputEnt1[testCase]
elif ent_order == 2:
return self.testInputEnt2[testCase]
elif ent_order == 3:
return self.testInputEnt3[testCase]
Or another one:或者另一个:
def setEntityValue(self, testCase, ent_order, value):
if ent_order == 1:
self.testResultEnt1[testCase] = value
elif ent_order == 2:
self.testResultEnt2[testCase] = value
elif ent_order == 3:
self.testResultEnt3[testCase] = value
Is there a simpler way of constructing this testInputEntX series in a better way?是否有更简单的方法以更好的方式构建此 testInputEntX 系列? I'm well aware of the fact that it's ideal to use other type of data structures where the values 1, 2, 3 can be used as another index and testInputEnt can be a list of series.
我很清楚这样一个事实,即使用其他类型的数据结构是理想的,其中值 1、2、3 可以用作另一个索引,而 testInputEnt 可以是系列列表。 But I will have to stick to these series for this application.
但是对于这个应用程序,我将不得不坚持使用这些系列。
It could be refactored like:它可以重构为:
def getEntityValue(self, testCase, ent_order):
return getattr(self, f'testInputEnt{ent_order}')[testCase]
#getattr(self, f'testInputEnt{ent_order}')[testCase] = value
But, why don't you use a pandas.DataFrame
instead of N pandas series?但是,为什么不使用
pandas.DataFrame
而不是 N pandas 系列呢? You can use DataFrame.at
to get and update a cell您可以使用
DataFrame.at
获取和更新单元格
def getEntityValue(self, testCase, ent_order):
return self.testResultEnt.at[ent_order, testCase]
#self.testResultEnt.at[ent_order, testCase] = value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.