Python pandas的问题

d
decadence
楼主 (北美华人网)
complete the function to extract a list of categorical feature names from a dataframe using a threshold to infer categorical variable based on the number of unique levels. Exclude the target variable and sort the resulting list
def extract_categorical_features(df, n_levels, target)  #write your code here   solution = None   return solution test_case = extract_categorical_features(churn_df, 6, ''Churn'')
怎么快速熟悉pandas 的各种function,比如这个题我看到都不知道怎么开始?谢谢
多谢大家的帮忙,这是我写的,看看这样对了么?谢谢。最后不清楚的是怎么sort,就用的sort_values.
import pandas as pd def extract_categorical_features(df, n_levels, target)  #write your code here for column in df:    if (df[column].nunique() < n_levels and column != target):      df2 = df[column]    solution = df2.sort_values    return solution test_case = extract_categorical_features(churn_df, 6, ''Churn'')
大喜妞
先说题目。
input三个变量: dataframe, 一个threshold,一个target column.
dataframe可以想象成一个表格。既然例子里的一个变量名是‘Churn’,那估计就是个用户订阅的信息,比如每一行是一个用户,然后每一列是一些features. 比如这样:
Id | registered_dt | paid_membership | daily_online_hours | location | churn 大喜妞 | 2018-01-01| 0 | 1 | US | 0
然后要求是找出哪些列是categorical variables。categorical varialble就是类别变量,基本就是无序,但是有类别,区别于连续变量(比如收入,比如身高)。我们是人类,有common sense,放眼一望就知道paid_membership, location,churn算是类别变量,但是机器不知道啊。于是你就根据每一列有多少个unique values来判断。比如registered_dt,大家可能跨度十几二十年,于是好几千个unique values。而location,可能几百个,paid_membership就两个, churn也是两个。于是你设置一个threshold,比如例子里的,6个unique values以下, 那么那一列就是个categorical variable。这样一来,只有paid_membership和churn是categorical variable.
最后一个要求就是排除目标列。为啥叫目标列呢,商业应用上喜欢predict churn,比如谁要退订membership了,这样客服部门可以给个库胖啊之类的。这里churn是目标,所以就算churn满足条件,也不应该被算在内。
最后你还要排个序。
就这样。
你要不要试试写一下。遇上麻烦大家接着帮你。


然后再说怎么熟悉python function。function是你定义的。无他,只能多想多写。 然后如果想熟悉python的各种包裹,成为一名合格的调包侠,那就靠多用多搜索,同时眼观六路耳听八方,了解一下新发布的著名包裹。 说白了就是多用。好像没有太多捷径。我来学习一下其他人的经验。

s
simbelmyne
一个library不会使用,第一件事就应该是去找官方documentation, 看入门指导
w
wfmlover
其实这些问题都能从stackoverflow上面得到答案 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.select_dtypes.html
直接有API可以用选出category column 其他的都很容易了,unique一下就是了
y
yangerchenamu
先说题目。
input三个变量: dataframe, 一个threshold,一个target column.
dataframe可以想象成一个表格。既然例子里的一个变量名是‘Churn’,那估计就是个用户订阅的信息,比如每一行是一个用户,然后每一列是一些features. 比如这样:
Id | registered_dt | paid_membership | daily_online_hours | location | churn 大喜妞 | 2018-01-01| 0 | 1 | US | 0
然后要求是找出哪些列是categorical variables。categorical varialble就是类别变量,基本就是无序,但是有类别,区别于连续变量(比如收入,比如身高)。我们是人类,有common sense,放眼一望就知道paid_membership, location,churn算是类别变量,但是机器不知道啊。于是你就根据每一列有多少个unique values来判断。比如registered_dt,大家可能跨度十几二十年,于是好几千个unique values。而location,可能几百个,paid_membership就两个, churn也是两个。于是你设置一个threshold,比如例子里的,6个unique values以下, 那么那一列就是个categorical variable。这样一来,只有paid_membership和churn是categorical variable.
最后一个要求就是排除目标列。为啥叫目标列呢,商业应用上喜欢predict churn,比如谁要退订membership了,这样客服部门可以给个库胖啊之类的。这里churn是目标,所以就算churn满足条件,也不应该被算在内。
最后你还要排个序。
就这样。
你要不要试试写一下。遇上麻烦大家接着帮你。


然后再说怎么熟悉python function。function是你定义的。无他,只能多想多写。 然后如果想熟悉python的各种包裹,成为一名合格的调包侠,那就靠多用多搜索,同时眼观六路耳听八方,了解一下新发布的著名包裹。 说白了就是多用。好像没有太多捷径。我来学习一下其他人的经验。


大喜妞 发表于 2021-07-04 00:54

请问一下这位MM,这种问题一般是用来面试什么类型的数据专才呢? 我认为自己可以运用SQL和Python处理数据,但是这句话看了之后完全无解呢:using a threshold to infer categorical variable based on the number of unique levels 这是需要补充什么方面的知识呢?
坨坨的多多
请问一下这位MM,这种问题一般是用来面试什么类型的数据专才呢? 我认为自己可以运用SQL和Python处理数据,但是这句话看了之后完全无解呢:using a threshold to infer categorical variable based on the number of unique levels 这是需要补充什么方面的知识呢?

yangerchenamu 发表于 2021-07-04 02:33

是因为不理解各种变量类型造成的不理解题目么?猜测mm可能不是学统计的而是半路转行的?那也许可以补一下统计基础
z
zejuanxiaoyu
请问一下这位MM,这种问题一般是用来面试什么类型的数据专才呢? 我认为自己可以运用SQL和Python处理数据,但是这句话看了之后完全无解呢:using a threshold to infer categorical variable based on the number of unique levels 这是需要补充什么方面的知识呢?

yangerchenamu 发表于 2021-07-04 02:33

你需要知道catrgorical variable跟其他类型变量的区别就明白这句话了 多上网搜搜 另外针对你的主贴 你需要大概知道你的function可以分解成几个基本步骤 每个基本步骤就算不知道用什么函数 但是可以google 对于基本操作不熟悉 需要看对应的官方文档 基本就是文档学习系统化点的函数操作和上网搜如何用函数解决具体问题交替进行 过一段时间就有提高了 我的感觉是学coding最好是task oriented 不要专门系统学习函数 因为学了很快忘记 实在没概念就看youtube的很短的简明教程大概了解一下 你这个问题 需要1.首先知道什么是cat var 和其他类型变量的区别 延展一下可以进一步看一下python都有什么变量类型 2.然后怎么filter dataframe columns 涉及到df column的selection等等操作 3.怎么找到不同列的unique value 这种df操作 4. 找到之后怎么sort 等等 每一个如果涉及到函数语法 看文档 例子也有初级的 复杂的需要根据你的需求放到google里面搜索 想学的稍微融会贯通一点可以看相关函数的youtube视频 多用用就有感觉了 开始是debug不熟悉会有比较大的心理压力 debug也可以搜的 看看有没有人有类似问题 有时候是你自己函数不小心写错了 也可能是版本的bug
l
longlongago0
科普Python?
D
DaHill
Mark
t
tiramixu
找个cheat sheet
I
Irene1314
最后的排序要按什么规则排序?output不都是满足条件的list of features吗?按number of unique value排序吗?原题目好像也没有说?
d
decadence
complete the function to extract a list of categorical feature names from a dataframe using a threshold to infer categorical variable based on the number of unique levels. Exclude the target variable and sort the resulting list 多谢大家的帮忙,这是我写的,看看这样对了么?谢谢。最后不清楚的是怎么sort,就用的sort_values.
import pandas as pd def extract_categorical_features(df, n_levels, target)  #write your code here for column in df:    if (df[column].nunique() < n_levels and column != target):      df2 = df[column]    solution = df2.sort_values    return solution test_case = extract_categorical_features(churn_df, 6, ''Churn'')
w
wfmlover
complete the function to extract a list of categorical feature names from a dataframe using a threshold to infer categorical variable based on the number of unique levels. Exclude the target variable and sort the resulting list 多谢大家的帮忙,这是我写的,看看这样对了么?谢谢。最后不清楚的是怎么sort,就用的sort_values.
import pandas as pd def extract_categorical_features(df, n_levels, target)  #write your code here for column in df:    if (df[column].nunique() < n_levels and column != target):      df2 = df[column]    solution = df2.sort_values    return solution test_case = extract_categorical_features(churn_df, 6, ''''Churn'''')
decadence 发表于 2021-07-04 10:55

非常错
for column in df:    if (df[column].nunique() < n_levels and column != target): # numeric或者boolean也可以只含几个unique值 df2 = df[column] # 这里只返回一个column    solution = df2.sort_values # 这里sort_values需要pass argument    return solution
d
decadence
回复 13楼wfmlover的帖子
谢谢mm 这样呢? def extract_categorical_features(df, n_levels, target)  #write your code here   for column in df:   if (column != target):     if(df[column].nunique() > n_levels):       df.drop('column', axis=1)    solution = df.sort(axis=0)   return solution test_case = extract_categorical_features(churn_df, 6, 'Churn')
大喜妞
最后的排序要按什么规则排序?output不都是满足条件的list of features吗?按number of unique value排序吗?原题目好像也没有说?
Irene1314 发表于 2021-07-04 09:43

这个是个好问题。如果是面试,可以问问面试官,你是让我按照column name排序还是unique values排序,升降?
g
gokgs
python 大概是最垃圾的语言了, 应该没有之一。 sigh.
w
wfmlover
回复 16楼gokgs的帖子
好用就行 python救了多少转行的人,简直再生父母

大喜妞
回复 13楼wfmlover的帖子
谢谢mm 这样呢? def extract_categorical_features(df, n_levels, target)  #write your code here   for column in df:   if (column != target):     if(df[column].nunique() > n_levels):       df.drop(''column'', axis=1)    solution = df.sort(axis=0)   return solution test_case = extract_categorical_features(churn_df, 6, ''Churn'')
decadence 发表于 2021-07-04 11:51

好几个问题:
df.drop(''column'', axis=1)  这句。 首先,‘column’不打引号,因为column本身已经是个string了。你一旦加了引号,这个function就会找名叫''column''的列。 你跑跑看上面这句,然后看看下个循环出个什么,你就知道问题所在了。可以用debug的工具来看输出,也可以加一个print ()之类的来看。 df.sort这句。两个问题。首先,现在sort会给你报‘DEPRECATED:’, 意思就是新版本有新的function name了,这个很快就要退出历史舞台了。其次,用老版本的sort,你要给个column name,不然python会和前面某位网友有同样的问题:你让我按照什么sort?
我建议自己建一个假设的df,跑一下你写的,根据error message的提示上网找找答案。你试试,我们再帮你看看?这个小题目确实涉及了不少dataframe的基础知识。 学会debug是一个非常有用的技能。
建df可以用下面这个: d = {''col1'': [1, 2, 3, 4, 5, 6], ''col2'': [1, 1, 1, 1, 1, 1], ''col3'': [10, 20, 30, 40, 50, 60], ''churn'': [''yes'', ''yes'', ''no'', ''no'', ''yes'', ''no'']} df = pd.DataFrame(d)

大喜妞
python 大概是最垃圾的语言了, 应该没有之一。 sigh.
gokgs 发表于 2021-07-04 13:13

先不讨论这个大问题。公司既然愿意用,楼主愿意学,无可厚非。
大喜妞
请问一下这位MM,这种问题一般是用来面试什么类型的数据专才呢? 我认为自己可以运用SQL和Python处理数据,但是这句话看了之后完全无解呢:using a threshold to infer categorical variable based on the number of unique levels 这是需要补充什么方面的知识呢?

yangerchenamu 发表于 2021-07-04 02:33

我觉得这题算是简单的data scientist面试题。考的都是经常用的dataframe manipulation。
你引用的这句: infer categorical variable, number of unique levels,这两个加起来就告诉你答案了啊。
你如果常用dataframe,就知道column 会有unique levels。然后了解categorial variable的特点,据可以知道这题想让你干嘛。
i
iheartnyc
绝大多数人也只是熟悉自己常用的function,遇到不常用的都是临时查doc。所以学会搜索比较重要。初学者可以先把所有的function都看看,背下来是不可能的,就算真背下来,不用也就忘了。
d
dana466
学习python
2
201120152019
好棒呀,Python真是边用边学
d
decadence
回复 18楼大喜妞的帖子
多谢mm指导,这回应该对了,除了sort顺序可能不符合要求。 import pandas as pd d = {"col1": [1, 2, 3, 4, 5, 6], "col2": [1, 1, 1, 1, 1, 1], "col3": [10, 20, 30, 40, 50, 60], "Churn": [''yes'', ''yes'',''no'',''yes'',''no'',''yes'']} churn_df = pd.DataFrame(d) print(churn_df) def extract_categorical_features(df, n_levels, target):  #write your code here   for col in df:     if (col != ''Churn''):         if(df[col].nunique() > n_levels):           del df[col]   solution = df.sort_values(by=''Churn'', axis=0, ascending=True)   return solution test_case = extract_categorical_features(churn_df, 3, ''Churn'') test_case

跑的结果:
大喜妞
回复 18楼大喜妞的帖子
多谢mm指导,这回应该对了,除了sort顺序可能不符合要求。 import pandas as pd d = {"col1": [1, 2, 3, 4, 5, 6], "col2": [1, 1, 1, 1, 1, 1], "col3": [10, 20, 30, 40, 50, 60], "Churn": [''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''yes'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''', ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''yes'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''',''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''no'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''',''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''yes'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''',''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''no'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''',''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''yes'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''']} churn_df = pd.DataFrame(d) print(churn_df) def extract_categorical_features(df, n_levels, target):  #write your code here   for col in df:     if (col != ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Churn''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''):         if(df[col].nunique() > n_levels):           del df[col]   solution = df.sort_values(by=''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Churn'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''', axis=0, ascending=True)   return solution test_case = extract_categorical_features(churn_df, 3, ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Churn'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''') test_case

跑的结果:
decadence 发表于 2021-07-04 15:14

You are getting there
最后一小步,仔细对照一下题目,看看要返回什么值,什么格式。
complete the function to extract a list of categorical feature names... Exclude the target variable and sort the resulting list

d
decadence
回复 25楼大喜妞的帖子
# complete the function to extract a list of categorical feature names from a # dataframe using a threshold to infer categorical variable based on the # number of unique levels. Exclude the target variable and sort the resulting list import pandas as pd d = {"col1": [1, 2, 3, 4, 5, 6], "col2": [1, 1, 1, 1, 1, 1], "col3": [10, 20, 30, 40, 50, 60], "Churn": ['yes', 'yes','no','yes','no','yes']} churn_df = pd.DataFrame(d) #print(churn_df) def extract_categorical_features(df, n_levels, target):  #write your code here   for col in df:     if (col != 'Churn'):         if(df[col].nunique() > n_levels):           del df[col]   aa=list(df.columns)   solution= sorted(aa, reverse=False)   return solution test_case = extract_categorical_features(churn_df, 3, 'Churn') test_case 返回的需要是list,多谢mm,这样应该就对了。 ['Churn', 'col2']
w
wintersweety
mark mark
y
yangerchenamu
回复 25楼大喜妞的帖子
# complete the function to extract a list of categorical feature names from a # dataframe using a threshold to infer categorical variable based on the # number of unique levels. Exclude the target variable and sort the resulting list import pandas as pd d = {"col1": [1, 2, 3, 4, 5, 6], "col2": [1, 1, 1, 1, 1, 1], "col3": [10, 20, 30, 40, 50, 60], "Churn": ['yes', 'yes','no','yes','no','yes']} churn_df = pd.DataFrame(d) #print(churn_df) def extract_categorical_features(df, n_levels, target):  #write your code here   for col in df:     if (col != 'Churn'):         if(df[col].nunique() > n_levels):           del df[col]   aa=list(df.columns)   solution= sorted(aa, reverse=False)   return solution test_case = extract_categorical_features(churn_df, 3, 'Churn') test_case 返回的需要是list,多谢mm,这样应该就对了。 ['Churn', 'col2']
decadence 发表于 2021-07-04 15:29

mm的code写的很棒呀,但为什么churn这个关键字会出现在function里呢?
大喜妞
loop有个bug.
应该是for col in df.columns

d
decadence
loop有个bug.
应该是for col in df.columns


大喜妞 发表于 2021-07-04 15:44

thanks a bunch. the result is the same using col in df.columns or col in df it didn't complain either way, I am using Jupyter notebook. :)
大喜妞
thanks a bunch. the result is the same using col in df.columns or col in df it didn't complain either way, I am using Jupyter notebook. :)
decadence 发表于 2021-07-04 15:53

哦,发现了。是if的逻辑错误了。
可以删去 if (col!='Churn')
然后filter一下aa。