按单元格分组的列直方图

2024-04-18 19:49:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在熊猫数据帧中有一列叫做Column_values。我想对这些值做一个直方图如下:

  1. alphabetic characters细胞分组
  2. alpha numeric chacaters的细胞分组
  3. 将只包含digits and special chars , ; / .的细胞分组

这是我的专栏

Column_values
hello
goodmorning
6,35
11,68
Yours
ok
2292
Question
number
those
937,99
and
1
620
amounts
ROB21
Pieces
designation
these
rates
13s
2
with
the

谢谢


Tags: and数据alphacolumn直方图细胞specialvalues
1条回答
网友
1楼 · 发布于 2024-04-18 19:49:20

首先,需要使用映射函数创建组,然后绘制直方图。函数按条件映射-在您的情况下:

def find_group(val):
    val = str(val)
    if val.isalpha():
        return 'Alpha'
    elif val.isalnum and any(c.isalpha() for c in val):
        return 'Alphanumeric'
    else:
        return 'Special'

转换采用以下方法:

df.Column_values.apply(find_group)

通过增加数值和绘图方法进行绘图:

df.Column_values.apply(find_group).value_counts().plot(kind='bar')

相关问题 更多 >