仅为某些特定元素添加数据框的值 - python

时间:2015-07-31 14:31:42

标签: python pandas dataframe

使用这两个数据框

df1 = pd.DataFrame({'c1':['a','b','c','d'],'c2':[10,20,10,22]})
df2 = pd.DataFrame({'c3':['e','f','a','g','b','c','r','j','d'],'c4':[1,2,3,4,5,6,7,8,9]})

我正在尝试将c4的值添加到df1仅适用c3c1中也存在的>>> df1 c1 c2 c4 a 10 3 b 20 5 c 10 6 d 22 9 中的元素:

df2 = pd.DataFrame({'c3':['e','f','a','g','b','c','r','j','d'],'c4':[1,2,3,4,5,6,7,8,9]},'c5':[10,20,30,40,50,60,70,80,90])

在熊猫中有一种简单的方法吗?

更新:

如果

>>> df1
  c1  c2  c4  c5
  a   10  3   30  
  b   20  5   50
  c   10  6   60
  d   22  9   90

我怎样才能达到这个效果?

>>> df1['c1'].map(df2.set_index('c3')['c4','c5'])

这样做的:

KeyError

给了我一个// 1 - Set up the three plots self.aaplPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO]; self.aaplPlot.identifier = CPDTickerSymbolAAPL; self.googPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor greenColor] horizontalBars:NO]; self.googPlot.identifier = CPDTickerSymbolGOOG; self.msftPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO]; self.msftPlot.identifier = CPDTickerSymbolMSFT; // 2 - Set up line style CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init]; barLineStyle.lineColor = [CPTColor lightGrayColor]; barLineStyle.lineWidth = 0.5; // 3 - Add plots to graph CPTGraph *graph = self.hostView.hostedGraph; CGFloat barX = CPDBarInitialX; NSArray *plots = [NSArray arrayWithObjects:self.aaplPlot, self.googPlot, self.msftPlot, nil]; for (CPTBarPlot *plot in plots) { plot.dataSource = self; plot.delegate = self; plot.barWidth = CPTDecimalFromDouble(CPDBarWidth); plot.barOffset = CPTDecimalFromDouble(barX); plot.lineStyle = barLineStyle; [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace]; barX += CPDBarWidth; }

1 个答案:

答案 0 :(得分:2)

您可以在map上设置索引后在df2['c4']上致电df2['c3'],这将执行查找:

In [239]:
df1 = pd.DataFrame({'c1':['a','b','c','d'],'c2':[10,20,10,22]})
df2 = pd.DataFrame({'c3':['e','f','a','g','b','c','r','j','d'],'c4':[1,2,3,4,5,6,7,8,9]})
df1['c4'] = df1['c1'].map(df2.set_index('c3')['c4'])
df1

Out[239]:
  c1  c2  c4
0  a  10   3
1  b  20   5
2  c  10   6
3  d  22   9