如何让Pandas Dataframe理解和评估一些表达式?

时间:2017-06-22 15:46:54

标签: python pandas dataframe data-analysis

假设我有一个这样的列 -

df = pd.DataFrame({'a': [1,2,3], 'b':[10,20,30], 'c':[8,9,10]}) 
here columns are -
c = ['a','b','c']
I can calculate different formulas like this -
df['sum'] = df[c].sum(axis=1)
df['avg'] = df[c].mean(axis=1)

现在我有一个文件,公式以下面的格式编写 -

1. SUM(df[c])
2. AVG(df[c]) + MAX(df[c])
3. AVG(SUM(df[c])) * 100
etc..
Now is it possible to make above expressions being calculated by pandas without  writing much logics in python code? 

1 个答案:

答案 0 :(得分:0)

Sub Vlookup()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Set sourceSheet = Worksheets("Data1")
Set outputSheet = Worksheets("Info2")
With sourceSheet
     SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With outputSheet
    OutputLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  'This is the line where I keep getting the error:
 .Range("G2:G" & OutputLastRow).Formula = _
             "=VLOOKUP(A2,'" & sourceSheet & "'!$A$2:$B$" & SourceLastRow & ",2,0)"
    End With
    End Sub

通过这些,你可以做你需要的一切。