簡體   English   中英

Python Pandas比較DataFrame單元格中的日期時間值

[英]Python Pandas comparing datetime values in DataFrame cells

我運行了兩組速度測試並將數據記錄到CSV文件中,然后我將其讀回並轉換為DataFrames。 當我顯示數據時它看起來像這樣,我有2套它; 一個用於測試#1,一個用於測試#2

DataFrame結果表示例

我想要做的是將測試#1'Time Elapsed'列的每個單元格與測試#2'Time Elapsed'列的相應單元格進行比較,並在新的DataFrame顯示中以百分比比較變化(即+ 1.05%或 - 4.72%)。 我不知道如何訪問這些單元格並對它們進行任何比較,因為它們是奇怪的數據類型?

為了生成性能表,我編寫了以下代碼:

import random
import datetime as dt
import pandas as pd
import logging
import platform, psutil, GPUtil
import csv

#for debugging purposes
logging.basicConfig(filename='stressTest_LOG.txt', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.disable(level=logging.DEBUG)

#enlarge pandas display area
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

def passGen(passLen, randSeed):
    # randSeed = None #None uses time stamp as a value
    # passLen = 15 #password length

    random.seed(a=randSeed, version=2)

    # populate lists with character ranges based of ASCII table
    letters = list(range(65, 91)) + list(range(97, 123))
    symbols = list(range(33, 48))
    numbers = list(range(48, 58))

    passCombined = letters + symbols + numbers
    random.shuffle(passCombined)

    # check if first element is from symbol list and if so replace with a number
    while passCombined[0] > 32 and passCombined[0] < 49:
        # print("First symbol: "+ str(chr(passCombined[0])))
        passCombined[0] = random.randint(48, 58)
        # print("Changed to: "+ str(chr(passCombined[0])))

    finalPassword = passCombined[slice(passLen)]

    return finalPassword


def showPass(password):
    if len(password) > 32:
        print("Invalid password length.\nHas to be less than 32 characters.")
        return -1

    print(''.join(str(chr(e)) for e in password))



####################################### Main #######################################

# Generate CSV file
with open('performanceResults2.csv', 'w', newline='') as f:

    #declare columns in CSV file and their order
    fieldnames = ['Action', 'Start Time', 'End Time', 'Time Elapsed', 'OS',
                  'System', 'RAM', 'CPU count', 'CPU freq', 'GPU']
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()

    #gather system information
    info_architecture = platform.architecture()[0]
    info_machine = platform.machine()
    info_node = platform.node()
    info_system = platform.platform()
    info_os = platform.system()

    if info_os == 'Darwin':
        info_os = 'macOS'

    info_release = platform.release()
    info_version = platform.version()
    info_processor = platform.processor()
    info_pythonCompiler = platform.python_compiler()

    # get RAM memory info
    mem = psutil.virtual_memory().total
    mem = str(mem/(1024.**3)) + 'GB'

    # get CPU info
    cpu_count = psutil.cpu_count()
    cpu_freq = psutil.cpu_freq().current
    cpu_freq = round(cpu_freq / 1000, 2)
    cpu_freq = str(cpu_freq) + 'GHz'

    # get GPU info
    # Works only with Nvidia gpus and is based on nvidia-smi command
    gpuinfo = GPUtil.getGPUs()

    if len(gpuinfo) == 0:
        gpuinfo = 'Unsupported GPU model'

    #run random password generator program
    counter = 10000
    testCounter = 0


    #print("Test #1 Start time: " + str(startTime))


    for i in range(0,5):

        startTime = dt.datetime.now()

        while counter > 0:
            pass1 = passGen(30, None)
            #showPass(pass1)
            logging.debug('counter is: ' + str(counter) + ', password: ' + str(pass1))
            counter -= 1

        endTime = dt.datetime.now()
        #print("Test #1 End time  : " + str(endTime))

        timeDelta = endTime - startTime
        #print ("Test #1 Time elapsed: " + str(timeDelta))
        testCounter += 1
        counter = 10000
        testCounterDisplay = 'Test #' + str(testCounter)

        writer.writerow({'Action': testCounterDisplay, 'Start Time': startTime, 'End Time': endTime,
                         'Time Elapsed': timeDelta, 'OS': info_os, 'System': info_system, 'RAM': mem,
                         'CPU count': cpu_count, 'CPU freq': cpu_freq, 'GPU': gpuinfo})

#read back in and display the results
file = pd.read_csv('performanceResults2.csv', delimiter=',')
print(file)

為了比較結果,我只得到了這個:

import pandas as pd
import numpy as np

#enlarge pandas display area
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

#read in data to compare
test1 = pd.read_csv('performanceResults1.csv', delimiter=',')
test2 = pd.read_csv('performanceResults2.csv', delimiter=',')

#check if dataframes are equal
equality = test1.equals(test2)
print('DataFrame equal: ', equality)


df1_filtered = pd.DataFrame(test1[['Time Elapsed']])
df2_filtered = pd.DataFrame(test2['Time Elapsed'])

有什么想法嗎?

沒有看到你的時間單元格格式很難提供幫助據我了解你的時間來自日期時間格式:

 dt.datetime.now()

如果你想轉換為pandas時間戳:

 pd.to_datetime(dt.datetime.now())

您可以在“開始時間”和“結束時間”列上運行此操作並重新分配它們。 檢查你的DataFrame上的.dtypes(),它可能是“對象”,然后運行:

DF['Start Time'] = pd.to_datetime(DF['Start Time'])

在此dtype之后應該是datetime64[ns] ,這將允許您進行計算。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM