简体   繁体   中英

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.

[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*

You can use the apply method of DataFrame (see this ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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