在熊猫数据框上应用功能

时间:2018-10-08 03:33:07

标签: python pandas

我正在使用各种搜索算法,但我想在数据框中使用它的另一列来使用它,这是代码。

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]

*我想使用上述所有函数,以便它们使用Size的v值并创建一个随机数组和数字来计算其余列。

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

当前我正在使用手动计算并将数据附加到数据框 我的问题是如何在python代码中应用自定义函数 我目前正在将所有这些计算手动添加到数据框中*

1 个答案:

答案 0 :(得分:0)

您可以使用apply的{​​{1}}方法(请参见this):

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))