私には3つの機能があります。 4つの列(MemberID、年、DSFS、DrugCount)でデータフレームを処理し、年ごとに分類された3つのデータフレーム、年を再フォーマットするヘルパー関数、年ごとにデータフレームを分類コード化する3つ目のデータフレームを返します。別の関数のデータフレームオブジェクトを使用するにはどうすればよいですか?
- 第3の関数の入力として、年ごとに分類されたデータフレームはどのように取るのですか?
df['DSFS'].unique()
を使用して固有のカテゴリのリストを検索したいと思います。私はdf
として何を使用しますか?
csvファイルのサンプル。
MemberID DSFS DrugCount 2 61221204 2- 3 months 1 8 30786520 1- 2 months 1 11 28420460 10-11 months 1 12 11861003 4- 5 months 1 14 66905595 6- 7 months 4
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)
def replaceMonth(string):
replace_map = {'0- 1 month' : "0_1", "1- 2 months": "1_2", "2- 3 months": "2_3", "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
def process_yearly_DrugCount(aframe):
processed_frame = None
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_Y1.drop('Year', axis=1, inplace=True)
# print DrugCount_Y1['DSFS'].unique
return processed_frame
「DrugCount.csv」のサンプルを追加できますか? – jezrael