Pandas:在列值中查找空格和不常用字符的最快方法是什么?
Pandas: What's the quickest way to find spaces + unusal characters in column values?
在 Pandas 中,我有一列 col_one 最初在每个单元格中包含逗号分隔值。
['a, b, e, g, o', 'a, b, d', 'a, b, c, f, g', 'a, b, c, f', 'a, c, e', 'a, b, c, o', 'b, c, h, n', 'a, b, c, g, o', 'a, b, c, f', 'a, b, c, g, h, o', 'b', 'a, b, f, m', 'a, b, c, g, h', 'a, b, d, f, g', 'a, c, n', 'j', 'b, c, f', 'a, b, g, l', 'b', 'b', 'a, b, d, e ', 'a, b, c', 'a, b, e, g', 'a, b, c, d, f, g', 'd, k, l', 'a, b, c, f, g ', 'a, b, c, f', 'a, b, c, d, g', 'b, d, e', 'b, d', 'a', 'b, o', 'c, o', 'b, c, o', 'c', 'a, g, i', 'b, c, n', 'a, b', 'b, c, o, n', 'b, c, h', 'a, b, c, f, g, h', 'a, b, c, d', 'a, b, d', 'a, e, g', 'a, b, c, e, g, k, m', 'b, c, o', 'a, b, f, k', 'd, l', 'a, b, l', 'a, b, c', 'a', 'c, d, g, l', 'b, d, e, o', 'b, d', 'a, b, c, d, e, f, o', 'b', 'a, b, c, f', 'b, c, g', 'b, c, g, k', 'a', 'c', 'b, c, o', 'b, c, n, o']
我用 str.split(', ').explode().value_counts() .reset_index()
来计算单个字母的数量。但在生成的 table 中,一些字母出现了两次,大概是因为字符串包含尾随的 space。 不幸的是,这些在生成的 table 的 Jupyter Notebook 显示中不可见,因为它们只是空白。
使用这个
col_one_list = df["letter"].tolist()
print (col_one_list)
给了我一个所有计算值的列表。在此列表中,我能够发现尾随 space(“g”)。但是我怎么能做得更好呢?
['b', 'a', 'c', 'g', 'd', 'f', 'o', 'e', 'n', 'h', 'l', 'k', 'm', 'j', 'g ', ' g', 'e ', 'i']
您可以将空格替换为 ''
然后继续
split-explode-value_counts
,或者你也可以使用get_dummies
:
s.str.replace('\s+', '').str.get_dummies(',').sum()
输出:
a 36
b 49
c 35
d 15
e 9
f 13
g 18
h 5
i 1
j 1
k 4
l 5
m 2
n 5
o 13
dtype: int64
我会查看您的分解系列,看看哪些值有尾随空格:
letter_series = pd.Series(['b', 'a', 'c', 'g', 'd', 'f', 'o', 'e', 'n', 'h', 'l', 'k', 'm', 'j', 'g ', ' g', 'e ', 'i'])
letter_series.str.endswith(' ')
或查看哪些值超过一个字符
letter_series.str.len()
在 Pandas 中,我有一列 col_one 最初在每个单元格中包含逗号分隔值。
['a, b, e, g, o', 'a, b, d', 'a, b, c, f, g', 'a, b, c, f', 'a, c, e', 'a, b, c, o', 'b, c, h, n', 'a, b, c, g, o', 'a, b, c, f', 'a, b, c, g, h, o', 'b', 'a, b, f, m', 'a, b, c, g, h', 'a, b, d, f, g', 'a, c, n', 'j', 'b, c, f', 'a, b, g, l', 'b', 'b', 'a, b, d, e ', 'a, b, c', 'a, b, e, g', 'a, b, c, d, f, g', 'd, k, l', 'a, b, c, f, g ', 'a, b, c, f', 'a, b, c, d, g', 'b, d, e', 'b, d', 'a', 'b, o', 'c, o', 'b, c, o', 'c', 'a, g, i', 'b, c, n', 'a, b', 'b, c, o, n', 'b, c, h', 'a, b, c, f, g, h', 'a, b, c, d', 'a, b, d', 'a, e, g', 'a, b, c, e, g, k, m', 'b, c, o', 'a, b, f, k', 'd, l', 'a, b, l', 'a, b, c', 'a', 'c, d, g, l', 'b, d, e, o', 'b, d', 'a, b, c, d, e, f, o', 'b', 'a, b, c, f', 'b, c, g', 'b, c, g, k', 'a', 'c', 'b, c, o', 'b, c, n, o']
我用 str.split(', ').explode().value_counts() .reset_index()
来计算单个字母的数量。但在生成的 table 中,一些字母出现了两次,大概是因为字符串包含尾随的 space。 不幸的是,这些在生成的 table 的 Jupyter Notebook 显示中不可见,因为它们只是空白。
使用这个
col_one_list = df["letter"].tolist()
print (col_one_list)
给了我一个所有计算值的列表。在此列表中,我能够发现尾随 space(“g”)。但是我怎么能做得更好呢?
['b', 'a', 'c', 'g', 'd', 'f', 'o', 'e', 'n', 'h', 'l', 'k', 'm', 'j', 'g ', ' g', 'e ', 'i']
您可以将空格替换为 ''
然后继续
split-explode-value_counts
,或者你也可以使用get_dummies
:
s.str.replace('\s+', '').str.get_dummies(',').sum()
输出:
a 36
b 49
c 35
d 15
e 9
f 13
g 18
h 5
i 1
j 1
k 4
l 5
m 2
n 5
o 13
dtype: int64
我会查看您的分解系列,看看哪些值有尾随空格:
letter_series = pd.Series(['b', 'a', 'c', 'g', 'd', 'f', 'o', 'e', 'n', 'h', 'l', 'k', 'm', 'j', 'g ', ' g', 'e ', 'i'])
letter_series.str.endswith(' ')
或查看哪些值超过一个字符
letter_series.str.len()