-1

我正在执行拆分应用组合以查找每个成员的总数量。我需要的数据框应该有14列:MemberID, DSFS_0_1, DSFS_1_2, DSFS_2_3, DSFS_3_4, DSFS_4_5, DSFS_5_6, DSFS_6_7, DSFS_7_8, DSFS_8_9, DSFS_9_10, DSFS_10_11, DSFS_11_12, DrugCount。但是,我没有得到第14个(DrugCount),有什么想法?可变joined输出所有14,但joined_grouped_add,其中我做聚合的功能,只返回13.在拆分应用组合中,在熊猫中忽略的列

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
import sys 
from sklearn.cross_validation import train_test_split 
from sklearn import linear_model 

# this function takes the drugcount dataframe as input and output a tuple of 3 data frames: DrugCount_Y1,DrugCount_Y2,DrugCount_Y3 
def process_DrugCount(drugcount): 
    dc = pd.read_csv("DrugCount.csv") 
    sub_map = {'1' : 1, '2':2, '3':3, '4':4, '5':5, '6':6, '7+' : 7} 
    dc['DrugCount'] = dc.DrugCount.map(sub_map) 
    dc['DrugCount'] = dc.DrugCount.astype(int) 
    dc_grouped = dc.groupby(dc.Year, as_index=False) 
    DrugCount_Y1 = dc_grouped.get_group('Y1') 
    DrugCount_Y2 = dc_grouped.get_group('Y2') 
    DrugCount_Y3 = dc_grouped.get_group('Y3') 
    DrugCount_Y1.drop('Year', axis=1, inplace=True) 
    DrugCount_Y2.drop('Year', axis=1, inplace=True) 
    DrugCount_Y3.drop('Year', axis=1, inplace=True) 
    return (DrugCount_Y1,DrugCount_Y2,DrugCount_Y3) 

# this function converts strings such as "1- 2 month" to "1_2" 
def replaceMonth(string): 
    replace_map = {'0- 1 month' : "0_1", "1- 2 months": "1_2", "2- 3 months": "2_3", "3- 4 months": '3_4', "4- 5 months": "4_5", "5- 6 months": "5_6", "6- 7 months": "6_7", \ 
        "7- 8 months" : "7_8", "8- 9 months": "8_9", "9-10 months": "9_10", "10-11 months": "10_11", "11-12 months": "11_12"} 
    a_new_string = string.map(replace_map) 
    return a_new_string 

# this function processes a yearly drug count data 
def process_yearly_DrugCount(aframe): 
    processed_frame = None 
    aframe.drop("Year", axis = 1, inplace = True) 
    reformed = aframe[['DSFS']].apply(replaceMonth) 
    gd = pd.get_dummies(reformed) 
    joined = pd.concat([aframe, gd], axis = 1) 
    joined.drop("DSFS", axis = 1, inplace = True) 
    joined_grouped = joined.groupby("MemberID", as_index = False) 
    joined_grouped_agg = joined_grouped.agg(np.sum) 
    print joined_grouped_agg 
    return processed_frame 
def main(): 
    pd.options.mode.chained_assignment = None 
    daysinhospital = pd.read_csv('DaysInHospital_Y2.csv') 
    drugcount = pd.read_csv('DrugCount.csv') 
    process_DrugCount(drugcount) 
    process_yearly_DrugCount(drugcount) 
    replaceMonth(drugcount['DSFS']) 

if __name__ == '__main__': 
    main() 
+0

哪里是林es调用函数? – Parfait

+0

@Parfait编辑它。 – squidvision

+0

太多了,这里有太多的帮助。我建议打破每个部分并添加打印语句以查看内容以查看删除列的位置。否则,请设置一个[可重现的示例](http://stackoverflow.com/help/mcve)。 – Parfait

回答

0

简而言之,DrugCount直接从CSV拉出未读取作为数字字段(INT /浮动) 。否则它将保留在.agg(np.sum)处理中。聚集之前检查D型,看看它是否是一个object型(即字符串列):

print joined['DrugCount'].dtype 

事实上,在你的process_DrugCount()功能,您明确转换DrugCount柱为整数,astype但不这样做的process_yearly_DrugCount()功能。在后者的功能和DrugCount运行同一行应保留在汇总和处理:

aframe['DrugCount'] = aframe['DrugCount'].astype(int) 

或者更好的是,在main()避免做转换两次,后者的功能:

drugcount['DrugCount'] = drugcount['DrugCount'].astype(int) 

而且,做笔记, read_csv()允许列类型明确指定其D型说法:

drugcount = pd.read_csv('DrugCount.csv', dtype={'DrugCount': np.int64})