【问题标题】:Data Analyzed by Python CSVPython CSV 分析的数据
【发布时间】:2018-06-12 06:29:44
【问题描述】:

我将一个 csv 转换为一个名为 a 的列表。我有一种方法可以通过条件对我的数据进行分类。问题是它不起作用。如果在我的所有Cliente 上都有任何称为“稳定”的元素,我将条件设置为'Estable',这不是我需要的,但对于所有没有'Estable' 的客户作为 AAA 和 BBB,我希望你把'NoAnalyzed' 我在代码下面解释。

import pandas as pd

a = [['Cliente', 'Fecha', 'Variables', 'Dia Previo', 'Mayor/Menor', 'Dia a Analizar', 'Analisis'], 
['AAA', '27/12/2017', 'ECPM_medio', '0.41', 'Dentro del Margen', '0.35', 'Incremento'],
['BBB', '27/12/2017', 'ECPM_medio', '1.06', 'Dentro del Margen', '1.06', 'Alerta'],
['CCC', '27/12/2017', 'ECPM_medio', '1.06', 'Dentro del Margen', '1.06', 'Estable']]



headers = a.pop(0)
df = pd.DataFrame(a, columns = headers)
df['Analisis']


for elemento in df['Analisis']:
    if elemento == 'Estable':
        df['Status'] = 'Stable: The client''s performance was Stable'
    else:
        df['Status'] = 'NoAnalyzed'


df1= df.groupby(['Cliente','Fecha', 'Status']).size()
df1

output:
>>>
Cliente  Fecha       Status                                    
AAA      27/12/2017  Stable: The clients performance was Stable    1
BBB      27/12/2017  Stable: The clients performance was Stable    1
CCC      27/12/2017  Stable: The clients performance was Stable    1

I need:
>>>
Cliente  Fecha       Status                                    
AAA      27/12/2017  NoAnalyzed    1
BBB      27/12/2017  NoAnalyzed    1
CCC      27/12/2017  Stable: The clients performance was Stable    1

【问题讨论】:

    标签: python python-2.7 pandas csv dataframe


    【解决方案1】:

    我相信你需要numpy.wheremap,因为在pandas 中最好避免循环,因为速度慢:

    mask =  df['Analisis'] == 'Estable'
    df['Status'] = np.where(mask, 'Stable: The client''s performance was Stable', 'NoAnalyzed')
    

    或类似的:

    d = {True: 'Stable: The client''s performance was Stable',False: 'NoAnalyzed'}
    df['Status'] = mask.map(d)
    
    print (df)
      Cliente       Fecha   Variables Dia Previo        Mayor/Menor  \
    0     AAA  27/12/2017  ECPM_medio       0.41  Dentro del Margen   
    1     BBB  27/12/2017  ECPM_medio       1.06  Dentro del Margen   
    2     CCC  27/12/2017  ECPM_medio       1.06  Dentro del Margen   
    
      Dia a Analizar    Analisis                                      Status  
    0           0.35  Incremento                                  NoAnalyzed  
    1           1.06      Alerta                                  NoAnalyzed  
    2           1.06     Estable  Stable: The clients performance was Stable  
    

    【讨论】:

      【解决方案2】:

      问题是您直接将单个值分配给列而不是列表/数组/系列。单个值在每一行中复制自身。我建议您制作一个列表并将其分配给您的 df['Status'] 列。

      status=[]
      for elemento in df['Analisis']:
          if elemento == 'Estable'
              status.append('Stable: The client''s performance was Stable')
          else:
              status.append('NoAnalyzed')
      
      df['Status'] = status
      

      这应该可行。

      【讨论】:

      • 我有这个错误:ValueError: Length of values does not match length of index
      • 不知何故,如果语句已从我的代码中删除。我编辑了代码。
      猜你喜欢
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 2016-05-28
      • 2016-07-04
      • 2022-01-24
      • 2017-02-07
      • 2018-07-14
      • 2020-08-08
      相关资源
      最近更新 更多