简体   繁体   English

在熊猫数据框上应用功能

[英]Applying Function on a pandas dataframe

I am using various search algorithm and I want to use it on data frame using other column here is the code. 我正在使用各种搜索算法,并且我想在数据框中使用其他列来使用它,这是代码。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time as t
def linear_search(arr, x):
    for i in range(len(arr)): 
        # comparing the array against the given parameter
        if arr[i] == x:
            # Return the parameter if the paramerS is true
            return x

    return -1;

def linear_search_while(arr,x):
    found = -1
    i = 0

    while i < len(arr) and found == -1:
        if arr[i] == x:
            return x
        i = i + 1


    return -1
def binary_search_while(alist, item):
    first = 0
    last = len(alist)-1
    found = -1

    while first<=last and not found:
        midpoint = (first + last)//2
        if alist[midpoint] == item:
            found = item
        else:
            if item < alist[midpoint]:
                last = midpoint-1
            else:
                first = midpoint+1

    return found




def binary_search_rec(arr, x):


    if len(arr) == 0 or (len(arr) == 1 and arr[0]!= x):
        return -1
           mid = arr[len(arr)// 2]


    if x == mid: return x
    if x < mid: return binary_search_rec(arr[:len(arr)// 2], x)
    if x > mid: return binary_search_rec(arr[len(arr)//2+1:], x)


def selectionSort(a):
    len_a = len(a) # getting the length of the array
    for i in range (0,len_a-1):
        minIndex = i 
        for j in range(i+1, len_a): 
           array
            if a[j] < a[minIndex]: 
                minIndex = j
        temp = a[i]
        a[i] = a[minIndex]
        a[minIndex] = temp
    return a  
def timer_Linear_Search(arr, x):
    arr = selectionSort(arr)
    start = t.clock()
    linear_search_while(arr,x)
    stop = t.clock()
    timer = stop - start
    return timer

def timer_Binary_Search(arr, x):
    arr = selectionSort(arr)
    start = t.clock()
    binary_search_while(arr,x)
    stop = t.clock()
    timer = stop - start
    return timer

def timer_Sort(arr):
    start = t.clock()
    selectionSort(arr)
    stop = t.clock()
    timer = stop - start
    return timer
def timer_Linear_S_Search(arr, x):
    start = t.clock()
    arr = selectionSort(arr)
    linear_search_while(arr,x)
    stop = t.clock()
    timer = stop - start
    return timer

def timer_Binary_S_Search(arr, x):
    start = t.clock()
    arr = selectionSort(arr)
    binary_search_while(arr,x)
    stop = t.clock()
    timer = stop - start
    return timer
calculation_df = pd.DataFrame()
calculation_df['Size'] = [512,1024, 2048,4096,8192]

*I want to use all the above functions such that they use thev value of Size and create a random array and number to calculate the rest of the columns. *我想使用上述所有函数,以便它们使用Size的v值并创建一个随机数组和数字来计算其余的列。

[The final data should look something like this][1]

currently i am using manual calculation and appending the data to the dataframe My question is how can I apply custom function in python code I am currently manually adding all these calculation to the dataframe* 目前,我正在使用手动计算并将数据附加到数据框。我的问题是如何在python代码中应用自定义函数?我目前正在将所有这些计算手动添加至数据框*

You can use the apply method of DataFrame (see this ): 您可以使用apply的方法, DataFrame (见 ):

def linear_search(arr, x):
    for i in range(len(arr)):
        if arr[i] == x:
            return x
    return -1;

df = pd.DataFrame(data=[[1,2],[3,4],[5,6]], columns=["a", "b"])

df.apply(lambda arr: linear_search(arr, 1))

Output: 输出:

a    1
b   -1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM