【发布时间】:2021-09-04 14:12:12
【问题描述】:
我已经对来自https://github.com/mikemooreviz/superstore 的文件“Sample - Superstore.csv”进行了查询,这将为我提供包含某种类型的字符串标准的每个案例的计数,以及没有任何之前的计数其他计数的标准。
要分析的字符串列是“CustomerName”。
基本上:计算全名以大写“A”开头的客户端数量、全名以小写“t”开头的客户端数量、全名以小写结尾的客户端数量"n",然后是全名不符合上述任何条件的客户端数。
这是 pandas 中的查询:
import pandas as pd;import numpy as np;import re;
df = pd.read_csv("path_of_csv_file",sep=";");
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None);
df['strings_conditions'] = np.where(
df['CustomerName'].str.startswith("A"),
'Starts with a capital A',
np.where(
df['CustomerName'].str.contains("t"),
'Has a non-capitalized t',
np.where(
df['CustomerName'].str.endswith("n"),
'Finishes with a non-capitalized n',
'Something else'
)
)
)
df_new = df.loc[:,['strings_conditions','CustomerName']].drop_duplicates().dropna()
df_new.groupby(['strings_conditions'])['strings_conditions'].count()
给出以下结果:
| strings_conditions | count |
|---|---|
| Finishes with a non-capitalized n | 100 |
| Has a non-capitalized t | 288 |
| Something else | 341 |
| Starts with a capital A | 64 |
但 SQLite 中的查询相同:
SELECT 'Finishes with a non-capitalized n' AS strings_conditions, count(*)
FROM (
SELECT CustomerName
FROM mag_correction
WHERE mag_correction.CustomerName glob "*n"
GROUP by CustomerName
)
UNION ALL
SELECT 'Has a non-capitalized t' AS strings_conditions, count(*)
FROM (
SELECT CustomerName
FROM mag_correction
WHERE mag_correction.CustomerName glob "*t*"
GROUP by CustomerName
)
UNION ALL
SELECT 'Something else' AS strings_conditions, count(*)
FROM (
SELECT CustomerName
FROM mag_correction
WHERE mag_correction.CustomerName NOT glob "A*"
AND mag_correction.CustomerName NOT glob "*t*"
AND mag_correction.CustomerName NOT glob "*n"
GROUP by CustomerName
)
UNION ALL
SELECT 'Starts with a capital A' AS strings_conditions, count(*)
FROM (
SELECT CustomerName
FROM mag_correction
WHERE mag_correction.CustomerName glob "A*"
GROUP by CustomerName
)
给我:
| strings_conditions | count |
|---|---|
| Finishes with a non-capitalized n | 187 |
| Has a non-capitalized t | 313 |
| Something else | 341 |
| Starts with a capital A | 64 |
并使用以下查询在 SQLite 中创建一个与 pandas 中的 df_new 完全相同的视图:
SELECT
CASE
WHEN CustomerName glob "*n"
THEN "Finishes with a non-capitalized n"
WHEN CustomerName glob "*t*"
THEN "Has a non-capitalized t"
WHEN CustomerName NOT glob "A*"
AND CustomerName NOT glob "*t*"
AND CustomerName NOT glob "*n"
THEN "Something else"
WHEN CustomerName glob "A*"
THEN "Starts with a capital A"
END strings_conditions
, CustomerName
FROM mag_correction
GROUP by CustomerName
然后查询它:
SELECT df_new.strings_conditions, count(*)
FROM df_new
GROUP by df_new.strings_conditions
再次给出一堆不同的结果(除了两行与其他 SQLite 查询相比):
| strings_conditions | count |
|---|---|
| Finishes with a non-capitalized n | 187 |
| Has a non-capitalized t | 234 |
| Something else | 341 |
| Starts with a capital A | 31 |
有人知道为什么所有 3 个案例的结果都不相同吗?
如果需要任何澄清,我很乐意提供更多。
【问题讨论】:
标签: pandas string sqlite group-by count