我有以下代码通过合并到 dfs 来创建 dfs,而 dfs 又按列“År”(挪威语为年份)(2011、2012、2013 等)分组
def create_output_from_subset(analysis_type):
unique_customers_subset = df.groupby('År')['Kundenavn'].nunique().to_frame()
analyses_count_subset = df.groupby('År')['Kundenavn'].count().to_frame()
output_subset = pd.merge(unique_customers_subset, analyses_count_subset, left_index = True, right_index = True)
return output_subset
Run Code Online (Sandbox Code Playgroud)
被调用的函数返回以下内容:
Run Code Online (Sandbox Code Playgroud)Customers Analyses År 2011.0 46 59 2012.0 80 156 2013.0 76 148 2014.0 69 108 2015.0 39 82 2016.0 42 90 2017.0 23 36
年份索引 ( År) 的格式为Float64Index显示 1 位小数。任何想法如何将其显示为int(无小数)?
我的问题是:使用通过初始化的字段编写Java类构造函数的最佳方法是什么stdin?
例如,假设我有一个Employee类似于以下的类:
Public class Employee {
private int empID;
private String empName;
private List<Role> empRoles;
{....}
}
Run Code Online (Sandbox Code Playgroud)
我可以为这堂课写下所有的设定者和吸气剂.当然,Role该类将拥有自己的文件.
还假设我为前两个字段创建了我的setter,如下所示,以便让最终用户初始化字段:
public void setEmpID() {
System.out.println("Please enter the employee ID");
Scanner s = new Scanner (System.in);
this.empID = s.nextInt();
public void setEmpName() {
System.out.println("Please enter the employee name");
Scanner s = new Scanner (System.in);
this.empName = s.next();
}
Run Code Online (Sandbox Code Playgroud)
然后:
Scanner我在每个setter中创建的对象移动到构造函数并将其作为setter的参数更好例如:
public void setEmpName(Scanner s) {
...
this.empName = s.next();
}
Run Code Online (Sandbox Code Playgroud)
如您所见,这可能是一个设计问题,而不仅仅是"编码". …
我有一个DataFrame吸毒者年龄的熊猫.我的问题:例如,有些年龄段用连字符分隔'50-64'.我想抓住连字符分隔数字的平均值并用它替换单元格.
1.有没有办法用某种循环或方法做到这一点?我不想简单地硬编码drugs.loc[10,'age'] = np.mean(55+64)
2.为了将来参考,是否有更优雅的方式处理带有连字符分隔数字的数据?
input:
drugs.age
output:
0 12
1 13
2 14
3 15
4 16
5 17
6 18
7 19
8 20
9 21
10 22-23
11 24-25
12 26-29
13 30-34
14 35-49
15 50-64
16 65+
input:
drugs.age.dtype
output:
dtype('O')
Run Code Online (Sandbox Code Playgroud) 给定一组整数1,2和3,找出这些可以加起来的方式的数量.(顺序很重要,即说n是5. 1 + 2 + 1 + 1和2 + 1 + 1 + 1是两个不同的解决方案)
我的解决方案涉及将n分成1的列表,因此如果n = 5,则A = [1,1,1,1,1].我将通过添加相邻的数字从每个列表递归地生成更多的子列表.所以A将产生4多个列表:[2,1,1,1],[1,2,1,1],[1,1,2,1],[1,1,1,2],且每个这些列表将生成进一步的子列表,直到它到达[3,2]或[2,3]这样的终止案例
这是我提出的解决方案(在Python中)
ways = []
def check_terminating(A,n):
# check for terminating case
for i in range(len(A)-1):
if A[i] + A[i+1] <= 3:
return False # means still can compute
return True
def count_ways(n,A=[]):
if A in ways:
# check if alr computed if yes then don't compute
return True
if A not in ways: # check for duplicates
ways.append(A) # global ways …Run Code Online (Sandbox Code Playgroud) python ×3
pandas ×2
algorithm ×1
constructor ×1
hyphen ×1
java ×1
numbers ×1
python-3.x ×1
recursion ×1