59.0k views
1 vote
Solve each of the following exercises from Think Python by using Pandas vectorized operations (not for loops). Focus on writing a function to solve each problem; You should call that function in your unit tests, and you may also want to call it in your programs __main__ block, possibly accepting arguments from the command-line.

1. Write a program that reads words.txt and prints only the words with more than 20 characters (not counting whitespace).

1 Answer

3 votes

Final answer:

To use Pandas to print words with more than 20 characters from 'words.txt', we import Pandas, read the file into a Series, and implement a vectorized string length comparison to filter and print the long words.

Step-by-step explanation:

To solve the problem of reading words.txt and printing words that have more than 20 characters using Pandas vectorized operations, we'll write a function that leverages the Pandas library's efficiency. First, we need to import Pandas and read the text file into a pandas Series. Once loaded, we use a vectorized string operation to filter out words based on length and print them.

Example Code

Here is a Python function that performs the required operation:

import pandas as pd
def find_long_words(file_path):
# Read the file into a Series
words = pd.read_csv(file_path, header=None, squeeze=True)
# Vectorized operation to find words longer than 20 characters
long_words = words[words.str.len() > 20]
print(long_words)

To use this function, simply call find_long_words('words.txt') within your __main__ block or from the command line with the appropriate file path.

User Hj David Kearns
by
9.5k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.