我正在尝试根据用户从下拉框中所做的选择在 Django 中过滤和显示数据。我正在使用 ajax 调用向 Django 视图发送请求。例如,当用户选择航空公司 A 时,Ajax 将向 Django 后端发送该选择的“值”,该“值”是一个整数,以过滤数据并将其发送回前端。这是我的代码:
HTML:
<form method="GET">
<select id="airline-selected">
{% for airline in airline_list %}
<option value="{{ airline.id }}">
{{ airline.name }}
</option>
{% endfor %}
</select>
<input type="button" value="Update" id="selection-button" method="GET">
</form>
Run Code Online (Sandbox Code Playgroud)
阿贾克斯:
<script>
$( "#selection-button" ).click(function(event) {
event.preventDefault();
var airlineSelected = $('#airline-selected').find(":selected").val();
$.ajax({
url: "{% url 'charts_data' %}",
method: 'GET',
filter_category: parseInt(airlineSelected),
success: function(data){
console.log(data)
},
error: function(error_data){
console.log("error")
console.log(error_data)
}
})
});
</script>
Run Code Online (Sandbox Code Playgroud)
视图.py:
class ChartData(generics.ListAPIView):
serializer_class = FinancialDataSerializer …Run Code Online (Sandbox Code Playgroud) 在 Pandas 中,我正在转置数据并想要命名该列。
我当前的数据是:
alpha bravo charlie
0 public private public
1 prodA prodB prodB
2 100 200 300
Run Code Online (Sandbox Code Playgroud)
转置并重命名列后,输出为:
df.transpose()
df.columns = ["category", "product", "price"]
category product price
alpha public prodA 100
bravo private prodB 200
charlie public prodB 300
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到预期的输出,例如:
company category product price
alpha public prodA 100
bravo private prodB 200
charlie public prodB 300
Run Code Online (Sandbox Code Playgroud) 我有以下日期字符串,"2018-05-08"我想将其转换为 python 的日期时间格式,例如:"2018-05-08T00:00:00.0000000". 我知道我可以组合这样的字符串:
> date = "2018-05-08"
> time = "T00:00:00.0000000"
> date+time
'2018-05-08T00:00:00.0000000'
Run Code Online (Sandbox Code Playgroud)
但是有没有pythonic的方法可以使用像datetime这样的库?
我有一堆需要在 Pandas 中清理的列。我写了一个函数来进行清理。我不确定如何将相同的功能应用于许多列。这是我正在尝试的:
df["Passengers", "Revenue", "Cost"].apply(convert_dash_comma_into_float)
Run Code Online (Sandbox Code Playgroud)
但我收到了 KeyError。
我正在尝试将sklearn SVR用于小型数据集.当我尝试拟合()数据时,我收到错误
TypeError:必须是实数,而不是str
这是我的数据和代码:
Revenue Units Rev_per_unit
0 147754.0 8333629.0 17.73
1 126146.0 7601824.0 16.59
2 152385.0 8487163.0 17.95
3 138703.0 8170619.0 16.98
4 157860.0 8589258.0 18.38
5 159981.0 8634245.0 18.53
6 160006.0 9063836.0 17.65
7 143556.0 9315878.0 15.41
8 129380.0 9012887.0 14.35
9 135771.0 9370077.0 14.49
10 129593.0 9018405.0 14.37
11 123941.0 9410973.0 13.17
from sklearn.svm import SVR
df = pd.read_csv('revenue.csv')
X = df[['Revenue', 'Unit']]
y = df['Rev_per_unit']
X_train, X_test, y_train, y_test = train_test_split(X, y)
svr_reg = SVR(gamma='scale', C=1.0, …Run Code Online (Sandbox Code Playgroud) 我在 JS 中有以下对象:
[
{
"financial_year":1,
"mainline_revenue":18743.0,
"regional_revenue":2914.0,
"other_revenue":3198.0,
"non_operating_items":-1983.0
},
{
"financial_year":2,
"mainline_revenue":20218.0,
"regional_revenue":3131.0,
"other_revenue":3394.0,
"non_operating_items":-3233.0
},
{
"financial_year":3,
"mainline_revenue":30802.0,
"regional_revenue":6322.0,
"other_revenue":5526.0,
"non_operating_items":-1367.0
}
]
Run Code Online (Sandbox Code Playgroud)
Financial_year是我想用来过滤数据的唯一标识符。如何过滤数据,例如 Financial_year 为 2 并将其他值放入数组中?
我有以下对象:
var obj = {"last_name": "john", "age": 23, "city": "London"}
Run Code Online (Sandbox Code Playgroud)
我想在此对象的开头添加first_name的新元素,如下所示:
{"first_name": "Samuel", "last_name": "john", "age": 23, "city": "London"}
Run Code Online (Sandbox Code Playgroud)
我知道我可以做,obj.first_name = "Samuel"但它在对象的末尾添加了新元素.我怎样才能在开始时添加它?
在 python 中,如何在每 5 个项目之后创建一个列表列表?
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
Run Code Online (Sandbox Code Playgroud)
预期输出:
new_list = [['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j']....]
Run Code Online (Sandbox Code Playgroud) 我有以下数据:
product profit
a 32
b 43
c 23
d 34
e 0
f 0
g -14
h -12
i -9
j -8
k -3
Run Code Online (Sandbox Code Playgroud)
我想对值进行排序并忽略零。
df.sort_values(by="profit", ascending=False)
Run Code Online (Sandbox Code Playgroud)
预期的输出应该是:
product profit
b 43
d 34
a 32
c 23
k -3
j -8
i -9
h -12
g -14
Run Code Online (Sandbox Code Playgroud) 我想通过用户提供的 URL 过滤我的 Django Rest Framework 序列化数据。这是我的代码:
模型.py:
class Airline(models.Model):
name = models.CharField(max_length=10, blank=True, null=True)
code = models.CharField(max_length=2, blank=True, null=True)
def __str__(self):
return self.name
class FinancialData(models.Model):
airline = models.ForeignKey(Airline)
mainline_revenue = models.DecimalField(max_digits=7, decimal_places=2)
regional_revenue = models.DecimalField(max_digits=7, decimal_places=2)
other_revenue = models.DecimalField(max_digits=7, decimal_places=2)
total_revenue = models.DecimalField(max_digits=7, decimal_places=2)
def __str__(self):
return str(self.mainline_revenue)
Run Code Online (Sandbox Code Playgroud)
视图.py:
class ListAirlineFinancialData(generics.ListAPIView):
serializer_class = FinancialDataSerializer
def get_queryset(self, *args, **kwargs):
query_list = FinancialData.objects.filter(pk=airline_id)
Run Code Online (Sandbox Code Playgroud)
网址.py:
urlpatterns = [
url(r'^api/v1/airline/(?P<pk>\d+)/$', views.ListAirlineFinancialData.as_view(), name='airline_financial_data'),
]
Run Code Online (Sandbox Code Playgroud)
我应该在视图中编码什么来过滤以下 URL 的数据。 http://localhost:8000/api/v1/airline/3/
此时 Django 给了我一个错误,名称“airline_id”未定义我可以理解它希望我传递我数据库中的 airline_id …
python ×7
pandas ×3
django ×2
javascript ×2
ajax ×1
dataframe ×1
jquery ×1
scikit-learn ×1
sorting ×1