Hướng dẫn column as argument python - cột dưới dạng đối số python

Vì vậy, tôi có một lượng lớn các cột trong khung dữ liệu gấu trúc và tôi cần vượt qua các nhóm của chúng thông qua một chức năng. Hàm lớn nhưng tôi sẽ tạo một ví dụ dưới đây. Tôi không chắc chắn làm thế nào để chuyển tham chiếu của df.varname cho chức năng mà không gặp vấn đề về biến không được xác định. Khi tôi thử một chức năng như:

Nội phân chính

  • Làm thế nào để bạn áp dụng một hàm cho cột DataFrame?
  • Chúng ta có thể chuyển DataFrame cho chức năng không?
  • Làm cách nào để vượt qua một cột cụ thể trong gấu trúc?
  • Làm thế nào để bạn sử dụng khung dữ liệu trong một chức năng?

def bianco2(df, varX, varT):
    stdX = np.std(df.varX)
    stdT = np.std(df.varT)
    newVar = stdX + stdT
    return newVar

Tôi nhận được lỗi mà VARX không được xác định. Vì vậy, tôi đã viết chức năng nơi tôi sẽ vượt qua toàn bộ cụm từ:

def bianco3(varX, varT):
    stdX = np.std(varX)
    stdT = np.(varT)
    newVar = stdX + stdT
    return newVar

Trong đó "varx = df.varx".

Điều này đã hoạt động nhưng không thực tế cho một số lượng lớn các biến vì tôi vẫn phải cập nhật thủ công từng VARX và VART. Vì vậy, tôi đã thử tạo một danh sách các biến trong định dạng df.varx và sau đó sử dụng một vòng lặp để vượt qua danh sách các biến. Vấn đề là Python coi đó là một chuỗi và không phải là một tài liệu tham khảo. Tôi đã xem xét sử dụng functools.partial nhưng không thành công.

Bất kỳ ý tưởng nào về cách viết điều này theo định dạng đơn giản và có thể chuyển các cột gấu trúc cho một chức năng?

Đã hỏi ngày 22 tháng 2 năm 2017 lúc 21:13Feb 22, 2017 at 21:13

1

Bạn có thể muốn thử cái này?

def bianco2(df, varX, varT):
    stdX = np.std(df[varX])
    stdT = np.std(df[varT])
    newVar = stdX + stdT
    return newVar

print bianco2(df,'Customer','Policy')

đầu vào

   Policy  Customer  Employee CoveredDate   LapseDate
0     123      1234      1234  2011-06-01  2013-01-01
1     124      1234      1234  2016-01-01  2013-01-01
2     124      5678      5555  2014-01-01  2013-01-01

đầu ra

  2095.39309492

Đã trả lời ngày 22 tháng 2 năm 2017 lúc 21:20Feb 22, 2017 at 21:20

ShijoshijoShijo

8.6752 Huy hiệu vàng17 Huy hiệu bạc 30 Huy hiệu Đồng2 gold badges17 silver badges30 bronze badges

1

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    ĐọcDataframe/series.apply() method to apply a function.

    Bàn luận Dataframe/series.apply(func, convert_dtype=True, args=())

    Trong bài viết này, chúng tôi sẽ tìm hiểu các cách khác nhau để áp dụng một hàm cho các cột hoặc hàng được chọn hoặc được chọn trong DataFrame. Chúng tôi sẽ sử dụng phương thức dataFrame/series.apply () để áp dụng một hàm. This method will take following parameters :
    func: It takes a function and applies it to all values of pandas series.
    convert_dtype: Convert dtype as per the function’s operation.
    args=(): Additional arguments to pass to function instead of series.

    Cú pháp: dataFrame/series.apply (func, convert_dtype = true, args = ()) Pandas Series after applied function/operation.

    Tham số: Phương thức này sẽ thực hiện các tham số sau: Func: Nó có hàm và áp dụng nó cho tất cả các giá trị của pandas series.convert_dtype: Chuyển đổi DTYPE theo chức năng Hoạt động của chức năng.Args = (): Đối số bổ sung để chuyển sang chức năng thay vì chuỗi. Using Dataframe.apply() and lambda function.
    Example 1: For Column

    Loại trả lại: Sê -ri Pandas sau khi chức năng/hoạt động ứng dụng.

    Phương pháp 1: Sử dụng Dataframe.apply() và ________ 6. Ví dụ 1: cho cột

    import pandas as pd

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    4
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    5

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    6
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    7

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    1
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    3
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
      2095.39309492
    
    4
      2095.39309492
    
    5

    lambda function7

      2095.39309492
    
    6
      2095.39309492
    
    7
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1Dataframe.apply()1Dataframe.apply()2

    Hướng dẫn column as argument python - cột dưới dạng đối số python

    Is For Row.

    Loại trả lại: Sê -ri Pandas sau khi chức năng/hoạt động ứng dụng.

    Phương pháp 1: Sử dụng Dataframe.apply() và ________ 6. Ví dụ 1: cho cột

    import pandas as pd

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    4
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    5

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    6
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    7

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    1
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    3
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
      2095.39309492
    
    4
      2095.39309492
    
    5

      2095.39309492
    
    6
      2095.39309492
    
    7
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1Dataframe.apply()1Dataframe.apply()2

    lambda function7

      2095.39309492
    
    6
      2095.39309492
    
    7
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1Dataframe.apply()1Dataframe.apply()2

    Is Using

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    34 & [ ] Operator.

    Đầu ra: For Column.

    Loại trả lại: Sê -ri Pandas sau khi chức năng/hoạt động ứng dụng.

    Phương pháp 1: Sử dụng Dataframe.apply() và ________ 6. Ví dụ 1: cho cột

    import pandas as pd

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    4
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    5

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    6
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    7

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    1
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    3
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
      2095.39309492
    
    4
      2095.39309492
    
    5

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    90

      2095.39309492
    
    6
      2095.39309492
    
    7
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1Dataframe.apply()1Dataframe.apply()2

    Is For Row.

    Loại trả lại: Sê -ri Pandas sau khi chức năng/hoạt động ứng dụng.

    Phương pháp 1: Sử dụng Dataframe.apply() và ________ 6. Ví dụ 1: cho cột

    import pandas as pd

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    4
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    5

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    6
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    7

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    1
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    3
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
      2095.39309492
    
    4
      2095.39309492
    
    5

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    90

    Đầu ra:

    Phương pháp 3: Sử dụng phương pháp

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    47 và toán tử
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    48. Ví dụ 1: cho cột
    Using
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    47 method and
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    48 operator.
    Example 1: For Column

    import pandas as pd

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    1
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    3
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    Các

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    4
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    5

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    6
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    7

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
      2095.39309492
    
    4
      2095.39309492
    
    5

      2095.39309492
    
    6
      2095.39309492
    
    7
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1Dataframe.apply()1Dataframe.apply()2

    Các

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    02
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    03

    Đầu ra:

    Ví dụ 2: cho hàng. For Row.

    import pandas as pd

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    1
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    3
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    Các

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    6
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    7

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
      2095.39309492
    
    4
      2095.39309492
    
    5

      2095.39309492
    
    6
      2095.39309492
    
    7
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1Dataframe.apply()1Dataframe.apply()2

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    90

    Đầu ra:

    Các

    Ví dụ 2: cho hàng. For Column

    import pandas as pd

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    1
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    3
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    4
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    5

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    6
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    7

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
      2095.39309492
    
    4
      2095.39309492
    
    5

      2095.39309492
    
    6
      2095.39309492
    
    7
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1Dataframe.apply()1Dataframe.apply()2

    Các

    lambda function7

    Đầu ra:

    Ví dụ 2: cho hàng. For Row.

    import pandas as pd

    import

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    0

    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    1
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    3
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    6
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    0
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    5
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    4
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    5

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    6
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    7

    Các

      2095.39309492
    
    6
      2095.39309492
    
    7
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1Dataframe.apply()1Dataframe.apply()2

    Các

    Ví dụ 2: cho hàng.

    lambda function7

    Đầu ra:


    Các

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2

    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    37
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    25
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    83
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    52
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    25
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    01

    Chúng tôi cũng có thể áp dụng một hàm cho nhiều cột hoặc hàng trong DataFrame.

    Ví dụ 1: cho cột

    Is

       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    8
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    0
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
      2095.39309492
    
    2
    def bianco2(df, varX, varT):
        stdX = np.std(df[varX])
        stdT = np.std(df[varT])
        newVar = stdX + stdT
        return newVar
    
    print bianco2(df,'Customer','Policy')
    
    1
      2095.39309492
    
    4
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    9. The apply() method passes into the the string_to_float() function a row from the df dataframe one by one. The row['value'] is simply the row of the df dataframe that is being passed onto the function.

    Is

      2095.39309492
    
    83
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    29
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    2
    def bianco3(varX, varT):
        stdX = np.std(varX)
        stdT = np.(varT)
        newVar = stdX + stdT
        return newVar
    
    4
       Policy  Customer  Employee CoveredDate   LapseDate
    0     123      1234      1234  2011-06-01  2013-01-01
    1     124      1234      1234  2016-01-01  2013-01-01
    2     124      5678      5555  2014-01-01  2013-01-01
    
    5put the string name of the column in brackets. Returns a pandas series. Passing a list in the brackets lets you select multiple columns at the same time.

    Làm thế nào để bạn áp dụng một hàm cho cột DataFrame?

    Tạo một dữ liệu dạng bảng hai chiều, có kích thước, có khả năng không đồng nhất, DF ..The apply() function is used to apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).