我有3个函数:一个处理4列的数据帧(MemberID、year、DSFS和DrugCount)并返回3个按年份分类的数据帧,一个重新格式化year的helper函数,第三个按年份分类重新编码数据帧。你知道吗
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
你的例子我不太清楚,但这里有一个基于熊猫文档的稍微不同的例子,它展示了一些有用的技术:
听起来与其使用groupby,不如使用数据透视表重新形成多索引。你知道吗
例如,尝试:
测试df,然后比较测向头():
有印刷品数据透视表(索引=(“变量”,“日期”))
然后你可以做df_枢轴.loc['A']给你:
你可以很容易地用几年的时间来适应你的例子。对于这种类型的操作,它比使用groupby更容易,而且它将所有数据保存在一个数据帧(视图)中。你知道吗
您还可以使用值计数来查找所有值及其频率。所以在我的例子中:
将返回一个序列:
如果我理解正确的话,这个系列的索引就是你的唯一值列表。所以呢
应该给你想要的。你知道吗
相关问题 更多 >
编程相关推荐