python - 使用映射函数在 pandas 列中绘制直方图

标签 python pandas matplotlib dataframe histogram

我有一个csv我用 pandas 处理的文件。该列名为 manual_raw_value我想检索此列中的唯一字符并创建 histogram .

为了检索所有唯一值,我执行了以下操作:

 unique_values = set(df.manual_raw_value.apply(list).sum())

{' ',
 '!',
 '"',
 '%',
 '&',
 "'",
 '(',
 ')',
 '*',
 '+',
 ',',
 '-',
 '.',
 '/',
 '0',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 '9',
 ':',
 '=',
 '>',
 '?',
 '@',
 '_',
 'a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'g',
 'h',
 'i',
 'j',
 'k',
 'l',
 'm',
 'n',
 'o',
 'p',
 'q',
 'r',
 's',
 't',
 'u',
 'v',
 'w',
 'x',
 'y',
 'z'}

这是数据

 manual_raw_value
    6,35
    11,68
    VOTRE
    AVEL AR VRO
    2292
    questions.
    nb
    les
    937,99
    à
    et
    TTC
    1
    620
    Echéance
    vos
    ROB21
    Pièce
    AGRIAL
    désignation
    des
    taux
    13s
    2
    par
    le
    mois,
    32
    21/07/2016
    FR
    au
    0
    téléphonique
    BROYEUR
    et
    ST
    TVA
    de
    des
    ECHEANCIER
    à
    ne
    lieu
    481,67
    N°0016
    de
    ministère
    de
    20/11/2015
    Si
    vous
    59
    cas
    EUR
    3.19
    2
    contrôle
    assurances
    BAS
    et
    4423873
    renseignements
    6104219
    C9DECOMPTEDIVERS
    6635
    DE
    10825

现在,因为我有unique values我想做一个直方图。 这是我尝试过的

  import pandas as pd
    def find_group(val):
        unique_values = set(df.manual_raw_value.apply(list).sum())
        for unique in unique_values:
            # get the number of occurence of all the unique values
            # then make a histogram



    df = pd.read_csv('words.csv',sep=',')
    df = df.astype(str)
    df.manual_raw_value=df.manual_raw_value.str.lower()
    df.manual_raw_value.apply(find_group)
    df.manual_raw_value.apply(find_group).value_counts().plot(kind='bar')

唯一值是函数返回的值 unique_values = set(df.manual_raw_value.apply(list).sum())分别是{' ', '!', '"', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', .....等等 。 现在看看手册 row values : 6,35 11,68 的两个值那么我们可以说1 appears twice 6 twice ',' twice 3 one time 5 one time

编辑-1 我尝试使用此代码来制作 alpha cells 出现次数的直方图, alpahnumeric细胞和special char cells

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')

现在我想在字符级别制作直方图:

  • 通过循环遍历每个单元格来获取列中的唯一字符。 (完成)

  • 统计这些字符在所有单元格中出现的次数并制作直方图。 #我被困住了 -一次

编辑2 让我们举一个实际的例子。假设我的专栏名为 Column_value

 Column_value
    hello
    good
    morning
    how 
    are 
    you

1-在每一行计算每个字符出现的次数

hello :  h=1 l=2 o=1 e=1
good :   g=1 o=2 d=1
morning : m=1 o=1 r=1 n=2 g=1
how: h=1 o=1 w=1 
are : a=1 r=1 e=1
you: y=1 o=1 u=1

2-求和得到每个字符在所有行中出现的次数

h=1+1=2
l=2
o=2+1+1+1=5
e=1+1=2
g=1
d=1

等等 现在,制作一个直方图 h=2、l=2、o=5、e=2、g=1、d=1

最佳答案

以OP为例。

import pandas as pd
words=["hello","good","morning","how","are","you"]
df=pd.DataFrame(words,columns=['words'])

pd.Series(list(df.words.str.cat())).value_counts().plot(kind="bar")

enter image description here
在 df.words.str.cat() 之后,还可以使用正则表达式来过滤字符

关于python - 使用映射函数在 pandas 列中绘制直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44499947/

相关文章:

python - 如何使用 python-pandas 同时分解两个数据框?

linux - 看不到 pylab 的情节

python - 查找公共(public)列表序列

python - 我可以在本地测试 AWS Glue 代码吗?

python - 是否有用于 Python 的异步非网络 I/O 框架?

python - DataFrame:N 个最大索引值(从 level=1)到 n 列

python - 在数据类型上使用 "switch"的 python 字典

python - 删除多行上给定条件的索引

python - 是否可以在 matplotlib (python) 中的数据之上绘制轮廓?

python - 强调图像中的蓝色