Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

पायथन पैंडास - अनुरोधित लेबल के लिए पूर्णांक स्थान प्राप्त करें और सटीक मिलान न होने पर पिछला इंडेक्स मान खोजें


अनुरोधित लेबल के लिए पूर्णांक स्थान प्राप्त करने और सटीक मिलान न होने पर पिछला इंडेक्स मान खोजने के लिए, index.get_loc() का उपयोग करें। पैरामीटर method को मान ffill पर सेट करें।

सबसे पहले, आवश्यक लाइब्रेरी इंपोर्ट करें:

import pandas as pd

पैंडास इंडेक्स बनाएं:

index = pd.Index([10, 20, 30, 40, 50, 60, 70])

पैंडास इंडेक्स प्रदर्शित करें:

print("Pandas Index...\n", index)

सटीक मिलान न होने पर पिछले इंडेक्स का स्थान प्राप्त करें। मान "ffill" का उपयोग get_loc() के "method" पैरामीटर का उपयोग करके सेट किया जाता है:

print("\nGet the location of the previous index if no exact match...\n", index.get_loc(45, method="ffill"))

उदाहरण

import pandas as pd
# Creating Pandas index
index = pd.Index([10, 20, 30, 40, 50, 60, 70])
# Display the Pandas index
print("Pandas Index...\n", index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n", index.size)
# get integer location from the given index
print("\nDisplay integer location from given index...\n", index.get_loc(20))
print("\nDisplay integer location from given index...\n", index.get_loc(50))
# Get the location of the previous index if no exact match
# The value is set "ffill" using the "method" parameter of the get_loc()
print("\nGet the location of the previous index if no exact match...\n", index.get_loc(45, method="ffill"))

आउटपुट

Pandas Index... Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64')
Number of elements in the index... 7
Display integer location from given index... 1
Display integer location from given index... 4
Get the location of the previous index if no exact match... 3
  1. - पायथन - इंडेक्स मान का गुणनफल खोजें और योग ज्ञात करें

    जब इंडेक्स मान का गुणनफल और योग ज्ञात करना हो, तो enumerate एट्रिब्यूट का उपयोग किया जाता है। उदाहरण नीचे इसका प्रदर्शन दिया गया है: my_list = [71, 23, 53, 94, 85, 26, 0, 8] print(The list is :) print(my_list) my_result = 0 for index, element in enumerate(my_list): my_result += (index + 1) * el

  1. - पायथन पैंडास - एक कॉलम का अधिकतम मान खोजें और उसकी संबंधित पंक्ति मान लौटाएं

    पैंडास में एक कॉलम का अधिकतम मान खोजने और उसकी संबंधित पंक्ति मान लौटाने के लिए, हम df.loc[df[col].idxmax()] का उपयोग कर सकते हैं। आइए एक उदाहरण लेकर इसे बेहतर ढंग से समझें। चरण दो-आयामी, आकार-परिवर्तनीय, संभावित विषम सारणीबद्ध डेटा, df बनाएं। इनपुट डेटाफ्रेम, df प्रिंट करें। एक वेरिएबल, col इनिशि

  1. - पायथन प्रोग्राम: Series में NaN मानों के इंडेक्स खोजें

    समस्या विवरण दिए गए Series में NaN (Not a Number) मान वाले सभी इंडेक्स प्रिंट करें। इनपुट Series 0 1.0 1 2.0 2 3.0 3 NaN 4 4.0 5 NaN dtype: float64 अपेक्षित आउटपुट index is 3 index is 5 समाधान दृष्टिकोण Series बनाएं (NaN के लिए np.nan उपयोग करें)। items() से इंडेक्स-मान जोड