-1

スプリット・アプライ・コンバインを行って各メンバーの合計数を求めています。私が必要とするデータフレームには、12列の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番目の1(DrugCount)、任意のアイデアを取得していないよ?すべての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

そしてlinがあります関数を呼び出すes? – Parfait

+0

@パフェット編集しました。 – squidvision

+0

あまりにも多くの人が手伝っています。私は、各部分を壊し、印刷文を追加して内容を見て、列が削除された場所を確認することをお勧めします。それ以外の場合は、[再現可能な例](http://stackoverflow.com/help/mcve)を設定します。 – Parfait

答えて

0

DrugCountを返す変数joined出力は数値フィールド(整数/浮動小数点数)として読んでされていないCSVファイルから直接取得しました。さもなければそれは.agg(np.sum)処理で保持されます。集約前DTYPEをチェックし、それがobjectタイプ(つまり、文字列の列)であるかどうかを確認:実際には

print joined['DrugCount'].dtype 

、あなたのprocess_DrugCount()機能では、明示的astypeで整数にDrugCount列を変換するが、中にそうしていませんprocess_yearly_DrugCount()機能。 、

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

また、ノートの操作を行います。main()に後者の機能に二回の変換を行うことを避けるために、まだ

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

またはそれ以上:後者の機能に同じラインを実行し、DrugCountは、合計額の処理中に保持されるべきですread_csv()はそのDTYPE引数を持つ列の型を明示的に指定することができます:

drugcount = pd.read_csv('DrugCount.csv', dtype={'DrugCount': np.int64}) 
関連する問題