vincent75
vincent75

Reputation: 473

histogram on pandas columns by grouping cells

l have a column in pandas dataframe called Column_values. l want to make a histogram on these values as follow :

  1. Grouping the cells of alphabetic characters
  2. Grouping the cells of alpha numeric chacaters
  3. Grouping the cells that contains only digits and special chars , ; / .

Here is my column

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

Thank you

Upvotes: 1

Views: 154

Answers (1)

Dimgold
Dimgold

Reputation: 2944

First, you need to create the group using a mapping function and then plot the histogram. The function is mapping by condition - in your case:

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'

the transformation is by using pandas apply method:

df.Column_values.apply(find_group)

and the plotting is by adding value_counts and plot methods:

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

Upvotes: 1

Related Questions