Complete the code to calculate the expanding sum of the 'sales' column.
import pandas as pd data = {'sales': [10, 20, 30, 40, 50]} df = pd.DataFrame(data) df['expanding_sum'] = df['sales'].[1]().sum() print(df)
The expanding() function creates an expanding window that includes all previous rows. Using sum() on it calculates the cumulative sum up to each row.
Complete the code to calculate the exponentially weighted mean with a span of 3.
import pandas as pd data = {'values': [1, 2, 3, 4, 5]} df = pd.DataFrame(data) df['ewm_mean'] = df['values'].[1](span=3).mean() print(df)
The ewm() function calculates exponentially weighted statistics. Setting span=3 controls the decay rate for weighting.
Fix the error in the code to calculate the expanding mean of the 'temperature' column.
import pandas as pd data = {'temperature': [22, 21, 23, 24, 25]} df = pd.DataFrame(data) df['expanding_mean'] = df['temperature'].[1]().mean print(df)
The mean is a method and needs parentheses () to be called. Using expanding() creates the expanding window.
Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.
words = ['data', 'science', 'is', 'fun'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3 using >.
Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.
data = {'a': 1, 'b': -2, 'c': 3}
result = {{ [1]: [2] for k, v in data.items() if v [3] 0 }}
print(result)The dictionary comprehension uses k.upper() for keys, keeps values v, and filters for values greater than zero with >.