def draw_piecharts(df, variables, n_rows, n_cols):
df[variables].value_counts.plot(kind='pie', layout=(n_rows,n_cols), subplots=True)
plt.show()
def main():
util.draw_piecharts(df, [ 'TARGET', 'BanruptcyInd'], 1,2)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
不幸的是,我的函数无法计算,因为数据框没有attribute value_counts(),而value_counts是我知道如何获取饼图中的分布的唯一方法。以下是绘制的变量的示例:
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 1
13 0
14 0
15 0
16 0
17 1
18 0
19 0
20 0
21 1
22 0
23 0
24 1
25 0
26 1
27 …Run Code Online (Sandbox Code Playgroud) 我是Python的新手,虽然我已经完成了对Codecademy的一些复习,但我在完成第一项任务时遇到了困难.
"任务1:请打开代码框架文件"i772_assg1.py"并实现函数list_ele_idx(li).您必须编写一个"for循环"来读取列表的元素,并为每个元素创建一个元组(元素) ,index)在列表中记录元素及其索引(位置).该函数以列表"li"作为参数.函数的返回值应该是(元素,索引)元组的列表.方法,我已经为任务1编写了一个测试用例.请取消评论测试用例并测试你的功能."
这是我正在尝试的,但我没有得到任何东西.任何反馈将不胜感激,因为这是我在Stack Overflow上的第一篇文章!
def list_ele_idx(li):
index = 0 # index
element = [(5,3,2,6)]
li = [] # initialze a list; you need to add a tuple that includes an element and its index to this list
# your code here. You must use for loop to read items in li and add (item,index)tuples to the list lis
for index in li:
li.append((element, index))
return li # return a list of tuples
Run Code Online (Sandbox Code Playgroud) 我正在编写一个用于日志转换的辅助类函数。它需要:
indexplus到原始变量的索引,并使其成为“log_variable”的索引。我将如何log(variable_value +1)处理缺失值或零值?我找到了使用的值isnull(),我可以以where.isnull()某种方式使用该函数吗?
我也很难将原始变量索引添加到 indexplus,并将其分配给 log_variable 的索引。
这是我的代码:
def add_log_transform(df, variable, indexplus = 1):
print df[variable].isnull()
log_variable = (np.log(df[variable]))
def main():
variables_needs_tranform = ['DerogCnt', 'CollectCnt', 'InqCnt06', 'InqTimeLast', 'InqFinanceCnt24', 'TLTimeFirst', 'TLTimeLast', 'TLCnt03', 'TLCnt12', 'TLCnt24', 'TLCnt', 'TLSum', 'TLMaxSum', 'TLDel60Cnt', 'TLBadCnt24', 'TL75UtilCnt', 'TL50UtilCnt', 'TLBalHCPct', 'TLSatPct', 'TLDel3060Cnt24', 'TLDel90Cnt24', 'TLDel60CntAll', 'TLBadDerogCnt', 'TLDel60Cnt24', 'TLOpen24Pct']
util.add_log_transform(df, variables_needs_tranform, indexplus = 1)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
这是我的 df 示例。
DerogCnt CollectCnt …Run Code Online (Sandbox Code Playgroud)