有没有更好的方法将数组中的元素逐个插入
到所有可能的位置(n + 1个位置).
例如,插入[1]到[6 7 8 9]应该产生:
[1 6 7 8 9]
[9 1 6 7 8]
[8 9 1 6 7]
[7 8 9 1 6]
[6 7 8 9 1]
Run Code Online (Sandbox Code Playgroud)
所以,如果我A = [1 2 3]逐个插入B = [6 7 8 9]它应该产生:
[1 6 7 8 9]
[9 1 6 7 8]
[8 9 1 6 7]
[7 8 9 1 6]
[6 7 8 9 1]
--------------------
[2 6 …Run Code Online (Sandbox Code Playgroud) 我在这里遇到角度2的麻烦。我使用返回承诺的服务,但是当我尝试检索响应时出现错误。
我读过这个问题, 这是我的代码。
这是HotelService.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
//rxjs promises cause angular http return observable natively.
import 'rxjs/add/operator/toPromise';
@Injectable()
export class HotelService {
private BASEURL : any = 'http://localhost:8080/hotel/';
constructor(private http: Http) {}
load(): Promise<any> {
return this.http.get(this.BASEURL + 'api/client/hotel/load')
.toPromise()
.then(response => {
response.json();
//console.log(response.json());
})
.catch(err => err);
}
}
Run Code Online (Sandbox Code Playgroud)
此Hotel.ts(组件)
import { Component, OnInit } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HotelService } from …Run Code Online (Sandbox Code Playgroud) 我有这种格式的用户名RSmith.我可以将它们放在所有upercase或标题中看起来像这样,Rsmith,但我需要RSmith.
当前脚本:
import csv
app_csv = ('/tmp/lowerAccounts.csv')
app_csv_upper = ('/tmp/Identity_Accounts.csv')
in_app_csv = open(app_csv)
out_app_csv = open(app_csv_upper, 'w')
inreader = csv.reader(in_app_csv)
outwriter = csv.writer(out_app_csv)
for row in inreader:
outwriter.writerow(row)
row[0] = row[0].upper()
outwriter.writerow(row)
row[0] = row[0].title(row)
outwriter.writerow(row)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用pip命令将 Python 3.6.3 更新到 3.6.5。但没有成功。我想知道是否可以使用pip更新 Python 。或者我必须从网站下载 Python 3.6.5?
我有以下数据帧:
Timestamp id lat long
0 665047 a 30.508420 -84.372882
1 665047 b 30.491882 -84.372938
2 2058714 b 30.492026 -84.372938
3 665348 a 30.508420 -84.372882
4 2055292 b 30.491899 -84.372938
Run Code Online (Sandbox Code Playgroud)
我希望的结果是:
Timestamp a b
0 665047 [30.508420, -84.372882] [30.491882, -84.372938]
1 665348 [30.508420, -84.372882] NaN
2 2055292 NaN [30.491899, -84.372938]
3 2058714 NaN [30.492026, -84.372938]
Run Code Online (Sandbox Code Playgroud)
在df.id成为列标题中找到的唯一值(可能有数千个),其纬度和经度为值.
我最接近的是使用:
for i, r in df.iterrows():
dct[r.Timestamp].append([r.id, r.lat, r.long])
pd.DataFrame.from_dict(dct, orient='index')
0 1
2055292 [b, 30.491899, -84.372938] None
2058714 [b, 30.492026, -84.372938] …Run Code Online (Sandbox Code Playgroud) 无论如何要根据另一个变量重命名值?在这里,我有两列,其中一列是 ID,另一列是水果。但是,我在想是否可以根据 ID 唯一地识别它们
ID Fruits
1 Apple
1 Banana
1 Orange
1 Banana
2 Apple
2 Orange
2 Orange
3 Apple
3 Apple
3 Orange
Run Code Online (Sandbox Code Playgroud)
希望实现这样的目标
ID Fruits
1 Apple
1 Banana
1 Orange
1 Banana1
2 Apple
2 Orange
2 Orange1
3 Apple
3 Apple1
3 Orange
Run Code Online (Sandbox Code Playgroud) 如何做这个列表/字典理解把这个 [("a", 1), ("b", 2), ("a", 3)]
进入这个
{
"a": [1, 3],
"b": [2]
}
Run Code Online (Sandbox Code Playgroud)
我知道如何在 for 循环中执行此操作,我可以只使用一行来完成这项工作吗?
这是我的桌子:
A B C E
0 1 1 5 4
1 1 1 1 1
2 3 3 8 2
Run Code Online (Sandbox Code Playgroud)
现在,我想按A列和B列对所有行进行分组。应该对C列求和,对于E列,我想使用C值为最大值的值。
我完成了将A和B分组并求和C的第一部分。
df = df.groupby(['A', 'B'])['C'].sum()
Run Code Online (Sandbox Code Playgroud)
但是在这一点上,我不确定如何告诉E列应采用C为最大值的值。
最终结果应如下所示:
A B C E
0 1 1 6 4
1 3 3 8 2
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决过去的问题吗?谢谢!
假设我有两个以下形式的字典:
{'A':[1,2,3,4,5,6,7],
'B':[12,13,14,15,16,17,18} - Belongs to category "M"
{'A':[8,9,10,11,12,13,14],
'B':[18,19,20,21,22,23,24]} - Belongs to category "P"
Run Code Online (Sandbox Code Playgroud)
现在生成的数据框应该是这样的形式——
Name . Value . Category
A . 1 . M
A . 8 . P
A . 10 . P
B . 12 . M
Run Code Online (Sandbox Code Playgroud)
等等。怎样才能实现这样的目标呢?
我有一个熊猫数据框df:
import pandas as pd
import numpy as np
data = {'A':[250,100,400,np.nan,300]}
df = pd.DataFrame(data)
print(df)
A
0 250.0
1 100.0
2 400.0
3 NaN
4 300.0
Run Code Online (Sandbox Code Playgroud)
我想根据列表(值)中的值来转换此数据仓库(DF)。
values = [0,200,400,600]
Run Code Online (Sandbox Code Playgroud)
在df中,第一个数字为250。它在list values中介于200和400之间,使得(| 200-250 |)/(400-200)= 0.25和(400-250)/(400-200)= 0.75。如果缺少数据(np.nan),则必须用0填充行。我要以这种方式将此表示为数据框的值进行转换。
所需的数据框:
0 200 400 600
0 0.0 0.25 0.75 0.0
1 0.5 0.50 0.00 0.0
2 0.0 0.00 1.00 0.0
3 0.0 0.00 0.00 0.0
4 0.0 0.50 0.50 0.0
Run Code Online (Sandbox Code Playgroud) python ×9
pandas ×5
python-3.x ×4
numpy ×2
algorithm ×1
angular ×1
dataframe ×1
duplicates ×1
ionic2 ×1
javascript ×1
matrix ×1
renaming ×1
string ×1
undefined ×1