शुरू करेंमुफ़्त में शुरू करें

सभी बातों को साथ लाना (2)

वाह! आपने पिछले अध्याय में किए गए अपने Twitter भाषा विश्लेषण को जनरलाइज़ कर दिया है और कॉलम नाम के लिए एक डिफ़ॉल्ट आर्ग्युमेंट जोड़ दिया है. अब आप इस फंक्शन को एक कदम और आगे बढ़ाएँगे ताकि यूज़र इसे फ्लेक्सिबल आर्ग्युमेंट पास कर सके — यानी इस मामले में, जितने कॉलम नाम यूज़र देना चाहे!

आपकी सुविधा के लिए, pandas को pd नाम से इम्पोर्ट किया गया है और 'tweets.csv' फ़ाइल को DataFrame tweets_df में लोड किया गया है. आपके पिछले कार्य के कुछ हिस्सों का कोड भी उपलब्ध कराया गया है.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में Functions का परिचय

पाठ्यक्रम देखें

अभ्यास निर्देश

  • फंक्शन हेडर को पूरा करें: DataFrame df के लिए पैरामीटर और फ्लेक्सिबल आर्ग्युमेंट *args दें.
  • फंक्शन डेफिनिशन के अंदर for लूप को पूरा करें ताकि लूप tuple args पर चले.
  • tweets_df DataFrame और कॉलम नाम 'lang' पास करके count_entries() कॉल करें. परिणाम को result1 में असाइन करें.
  • tweets_df DataFrame और कॉलम नाम 'lang' तथा 'source' पास करके count_entries() कॉल करें. परिणाम को result2 में असाइन करें.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

# Define count_entries()
def ____(____, ____):
    """Return a dictionary with counts of
    occurrences as value for each key."""
    
    #Initialize an empty dictionary: cols_count
    cols_count = {}
    
    # Iterate over column names in args
    for col_name in ____:
    
        # Extract column from DataFrame: col
        col = df[col_name]
    
        # Iterate over the column in DataFrame
        for entry in col:
    
            # If entry is in cols_count, add 1
            if entry in cols_count.keys():
                cols_count[entry] += 1
    
            # Else add the entry to cols_count, set the value to 1
            else:
                cols_count[entry] = 1

    # Return the cols_count dictionary
    return cols_count

# Call count_entries(): result1
result1 = count_entries(____, ____)

# Call count_entries(): result2
result2 = count_entries(____, ____, ____)

# Print result1 and result2
print(result1)
print(result2)
कोड संपादित करें और चलाएँ