Intern
If there are duplicate orders in the data, how do you count unique orders?
sobes.tech AI
Answer from AI
To count unique orders in data with duplicates, you need to use a method to remove duplicate records based on a key that uniquely identifies an order (for example, order_id). In SQL, this can be done using COUNT(DISTINCT order_id), and in Python with pandas — using the drop_duplicates() method or the nunique() function.
Example in SQL:
SELECT COUNT(DISTINCT order_id) AS unique_orders_count
FROM orders;
Example in Python with pandas:
import pandas as pd
df = pd.DataFrame({'order_id': [1, 2, 2, 3, 4, 4, 4]})
unique_orders = df['order_id'].nunique()
print(unique_orders) # Will output 4