Руководства для разработчиковFor pandas UsersСборник рецептов PandasСправочник по Pandas Типовые приёмы Pandas и их эквиваленты в DataStore. Большая часть кода работает без изменений! Загрузка данных Чтение CSV # Pandas import pandas as pd df = pd.read_csv("data.csv") # DataStore - same! from chdb import datastore as pd df = pd.read_csv("data.csv") Чтение нескольких файлов # Pandas import glob dfs = [pd.read_csv(f) for f in glob.glob("data/*.csv")] df = pd.concat(dfs) # DataStore - more efficient with glob pattern df = pd.read_csv("data/*.csv") Фильтрация Одно условие # Pandas and DataStore - identical df[df['age'] > 25] df[df['city'] == 'NYC'] df[df['name'].str.contains('John')] Несколько условий # AND df[(df['age'] > 25) & (df['city'] == 'NYC')] # OR df[(df['age'] < 18) | (df['age'] > 65)] # NOT df[~(df['status'] == 'inactive')] Использование функции query() # Pandas and DataStore - identical df.query('age > 25 and city == "NYC"') df.query('salary > 50000') isin() # Pandas and DataStore - identical df[df['city'].isin(['NYC', 'LA', 'SF'])] between() # Pandas and DataStore - identical df[df['age'].between(18, 65)] Выбор столбцов Один столбец # Pandas and DataStore - identical df['name'] df.name # attribute access Несколько столбцов # Pandas and DataStore - identical df[['name', 'age', 'city']] Выборка и фильтрация # Pandas and DataStore - identical df[df['age'] > 25][['name', 'salary']] # DataStore also supports SQL-style df.filter(df['age'] > 25).select('name', 'salary') Сортировка Один столбец # Pandas and DataStore - identical df.sort_values('salary') df.sort_values('salary', ascending=False) Несколько столбцов # Pandas and DataStore - identical df.sort_values(['city', 'salary'], ascending=[True, False]) Получение верхних/нижних N # Pandas and DataStore - identical df.nlargest(10, 'salary') df.nsmallest(5, 'age') GroupBy и агрегирование Простой пример GroupBy # Pandas and DataStore - identical df.groupby('city')['salary'].mean() df.groupby('city')['salary'].sum() df.groupby('city').size() # count Несколько агрегаций # Pandas and DataStore - identical df.groupby('city')['salary'].agg(['sum', 'mean', 'count']) df.groupby('city').agg({ 'salary': ['sum', 'mean'], 'age': ['min', 'max'] }) Именованные агрегации # Pandas and DataStore - identical df.groupby('city').agg( total_salary=('salary', 'sum'), avg_salary=('salary', 'mean'), employee_count=('id', 'count') ) Несколько ключей группировки # Pandas and DataStore - identical df.groupby(['city', 'department'])['salary'].mean() Объединение данных Внутреннее соединение # Pandas pd.merge(df1, df2, on='id') # DataStore - same API pd.merge(df1, df2, on='id') # DataStore also supports df1.join(df2, on='id') Левое соединение # Pandas and DataStore - identical pd.merge(df1, df2, on='id', how='left') Объединение по разным столбцам # Pandas and DataStore - identical pd.merge(df1, df2, left_on='emp_id', right_on='id') Объединение # Pandas and DataStore - identical pd.concat([df1, df2, df3]) pd.concat([df1, df2], axis=1) Операции со строками Изменение регистра # Pandas and DataStore - identical df['name'].str.upper() df['name'].str.lower() df['name'].str.title() Подстрока # Pandas and DataStore - identical df['name'].str[:3] # First 3 characters df['name'].str.slice(0, 3) Поиск # Pandas and DataStore - identical df['name'].str.contains('John') df['name'].str.startswith('A') df['name'].str.endswith('son') Замена # Pandas and DataStore - identical df['text'].str.replace('old', 'new') df['text'].str.replace(r'\d+', '', regex=True) # Remove digits Разделение # Pandas and DataStore - identical df['name'].str.split(' ') df['name'].str.split(' ', expand=True) Продолжительность # Pandas and DataStore - identical df['name'].str.len() Операции с датой и временем Выделение компонентов # Pandas and DataStore - identical df['date'].dt.year df['date'].dt.month df['date'].dt.day df['date'].dt.dayofweek df['date'].dt.hour Форматирование # Pandas and DataStore - identical df['date'].dt.strftime('%Y-%m-%d') Пропущенные данные Проверка пропущенных значений # Pandas and DataStore - identical df['col'].isna() df['col'].notna() df.isna().sum() Удаление пропущенных значений # Pandas and DataStore - identical df.dropna() df.dropna(subset=['col1', 'col2']) Заполнение пропущенных значений # Pandas and DataStore - identical df.fillna(0) df.fillna({'col1': 0, 'col2': 'Unknown'}) df.fillna(method='ffill') Создание новых столбцов Простое присваивание # Pandas and DataStore - identical df['total'] = df['price'] * df['quantity'] df['age_group'] = df['age'] // 10 * 10 Использование функции assign() # Pandas and DataStore - identical df = df.assign( total=df['price'] * df['quantity'], is_adult=df['age'] >= 18 ) Условные выражения (where/mask) # Pandas and DataStore - identical df['status'] = df['age'].where(df['age'] >= 18, 'minor') apply() для пользовательской логики # Works, but triggers pandas execution df['category'] = df['amount'].apply(lambda x: 'high' if x > 1000 else 'low') # DataStore alternative (stays lazy) df['category'] = ( df.when(df['amount'] > 1000, 'high') .otherwise('low') ) Изменение структуры данных Сводная таблица # Pandas and DataStore - identical df.pivot_table( values='amount', index='region', columns='product', aggfunc='sum' ) Melt (unpivot, обратное преобразование) # Pandas and DataStore - identical df.melt( id_vars=['name'], value_vars=['score1', 'score2', 'score3'], var_name='test', value_name='score' ) Развёртка (explode) # Pandas and DataStore - identical df.explode('tags') # Expand array column Оконные функции Скользящие окна # Pandas and DataStore - identical df['rolling_avg'] = df['price'].rolling(window=7).mean() df['rolling_sum'] = df['amount'].rolling(window=30).sum() Расширяющиеся окна # Pandas and DataStore - identical df['cumsum'] = df['amount'].expanding().sum() df['cummax'] = df['amount'].expanding().max() Смещение # Pandas and DataStore - identical df['prev_value'] = df['value'].shift(1) # Lag df['next_value'] = df['value'].shift(-1) # Lead Разница # Pandas and DataStore - identical df['change'] = df['value'].diff() df['pct_change'] = df['value'].pct_change() Результат В CSV # Pandas and DataStore - identical df.to_csv("output.csv", index=False) В Parquet # Pandas and DataStore - identical df.to_parquet("output.parquet") В DataFrame библиотеки pandas # DataStore specific pandas_df = ds.to_df() pandas_df = ds.to_pandas() Дополнительные возможности DataStore Просмотр SQL # DataStore only print(ds.to_sql()) План выполнения # DataStore only ds.explain() Функции ClickHouse # DataStore only - extra accessors df['domain'] = df['url'].url.domain() df['json_value'] = df['data'].json.get_string('key') df['ip_valid'] = df['ip'].ip.is_ipv4_string() Универсальный URI # DataStore only - read from anywhere ds = DataStore.uri("s3://bucket/data.parquet") ds = DataStore.uri("mysql://user:pass@host/db/table")