熊猫:如何创建一个运行计数列?

时间:2018-02-02 18:38:57

标签: python pandas reshape

我有一个表单的平面文本文件(由我添加的列标题)

CASE        Diagnosis
  S1 no diagnosis
  S2 fungus
     squamous lesion
  S3 fungus
  S4 squamous lesion
     glandular lesion
     atypia

我想堆叠和取消堆叠多个诊断的病例,所以我想

CASE DxN         Diagnosis
  S1 A   no diagnosis
  S2 A   fungus   
     B   squamous lesion
  S3 A   fungus
  S4 A   squamous lesion
     B   glandular lesion
     C   atypia

CASE                 A                 B       C
  S1 no diagnosis
  S2 fungus             squamous lesion
  S3 fungus
  S4 squamous lesion    glandular lesion  atypia

如何制作DxN子系列?计数永远不会大于F.即使有10,000个可能的答案,每个案例也不会超过6个,所以不超过6个列。我只想要"案例S1的诊断A是什么,案例S1的诊断B是什么,案例S1的诊断3是什么?"我不希望每个答案都有一个专栏。

3 个答案:

答案 0 :(得分:2)

这是你需要的吗?

    df=df.replace('',np.nan).ffill()
    df.assign(DxN=df.groupby('CASE').cumcount()).set_index(['CASE','DxN']).Diagnosis.unstack(fill_value='')
    Out[709]: 
    DxN                0                1
    CASE                                 
    S1       nodiagnosis                 
    S2            fungus   squamouslesion
    S3            fungus                 
    S4    squamouslesion  glandularlesion

答案 1 :(得分:1)

这是一种方法,从您拥有的文本格式的数据开始:

import pandas as pd
import numpy as np

df = pd.DataFrame([['S1', 'no diagnosis'],
                   ['S2', 'fungus'],
                   ['', 'squamous lesion'],
                   ['S3', 'fungus'],
                   ['S4', 'squamous lesion'],
                   ['', 'glandular lesion']],
                  columns=['CASE', 'Diagnosis'])

# front fill CASE series
df.CASE = df.CASE.replace('', np.nan).ffill()

# pivot data
df = pd.pivot_table(df, index=['CASE'], values=['Diagnosis'],
                    aggfunc=lambda x: list(x)).reset_index()

# split columns of lists into separate columns
df = pd.concat([df[['CASE']], pd.DataFrame(df['Diagnosis'].values.tolist())], axis=1)

#   CASE                0                 1
# 0   S1     no diagnosis              None
# 1   S2           fungus   squamous lesion
# 2   S3           fungus              None
# 3   S4  squamous lesion  glandular lesion

答案 2 :(得分:0)

您可以为每种情况创建一个包含诊断总计的列。有关详情,请参阅此帖子:SQL-like window functions in PANDAS: Row Numbering in Python Pandas Dataframe

使用此示例数据:

df = pd.DataFrame([
    {'Case': 'S1', 'Diagnosis': 'no diagnosis'},
    {'Case': 'S2', 'Diagnosis': 'fungus'},
    {'Case': 'S2', 'Diagnosis': 'squamous lesion'}
])

此脚本将为您提供运行总计:

df['DxN'] = df.sort_values(['Case'], ascending=[1]).groupby('Case').cumcount() + 1