how to count items in pandas dataframe
- User
how to count items in pandas dataframe
Assistant
To count items in a Pandas DataFrame, you can use the
nunique()function. This function returns the number of unique values in a column or DataFrame. Here's an example:import pandas as pd # Create a sample DataFrame data = {'A': ['apple', 'banana', 'apple', 'cherry', 'banana'], 'B': ['apple', 'banana', 'cherry', 'cherry', 'apple']} df = pd.DataFrame(data) # Count unique values in column 'A' count_A = df['A'].nunique() print(count_A) # Output: 3 # Count unique values in DataFrame df count_df = df.nunique() print(count_df) # Output: 4In this example,
count_Awill output 3 because there are 3 unique values in column 'A', andcount_dfwill output 4 because there are 4 unique values in the entire DataFrame df.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.