如何修改熊猫系列的价值

时间:2018-06-28 12:19:40

标签: python pandas

因此,我有一个带有一系列值的熊猫系列,这些值在循环中经过了几次。 每次我想在该索引处向该系列添加新内容时。

例如 我有一个称为df的大数据框。然后,我创建一个新列,并尝试在名为someList的列表的交互中满足条件的情况下对其进行修改。

MLT <- ggplot(m_ML, aes(x= year, y= value, group= groups, fill= variable)) + 
        geom_bar(stat="identity",position="dodge") + 
        theme_economist() + scale_colour_economist() + 
        coord_cartesian(ylim = c(0, 5000))

基本上,我想将另一个字符串连接到现有系列。我似乎找不到最佳的方法来做到这一点。我写的方法行得通,但也给了我一个熊猫警告:

df["mySeries"] = ""
for a,b in itertools.combinations(someList, 2):
  if int1>0.5:
    df["mySeries"][someList.index(a)] = df["mySeries"][someList.index(a)], b

并且输出不是干净的,因为它看起来像这样:(('',61),94) 我希望它看起来更像61、94。 我已经尝试过set_value,但这会覆盖系列中的现有数据。

1 个答案:

答案 0 :(得分:0)

如回溯所示,您需要使用.loc

>>> df.loc[someList.index(a), "mySeries"] = ...