我有一个数据框,我试图计算每个值的出现次数。我将其绘制为水平条,但无法对其进行排序。
df = pd.DataFrame(['A','A','A','B','B','C'],columns = ['letters'])
df.value_counts()
A 3
B 2
C 1
Run Code Online (Sandbox Code Playgroud)
我怎样才能以降序方式对其进行排序?
我怎么在vi中这样做?
awk -F"," awk '{print substr($1,1,10)}'
Run Code Online (Sandbox Code Playgroud)
我只想保留我的日期列的前10个字符(例如2014-01-01)并且不包括时间戳.
我试图在awk中做到这一点,但我得到了这个错误:
sed: RE error: illegal byte sequence
Run Code Online (Sandbox Code Playgroud)
我相信这是一个bash_profile设置错误.
这就是我在bash_profile中的含义:
#export LANG=en_US.UTF-8
#export LOCALE=UTF-8
export LC_CTYPE=C
export LANG=C
Run Code Online (Sandbox Code Playgroud) 我试图找到每个user_id的平均每月费用,但我只能获得每位用户的平均费用或每位用户的每月费用.
因为我按用户和月分组,除非我将groupby输出转换为其他内容,否则无法获得第二个groupby(月)的平均值.
这是我的df:
df = { 'id' : pd.Series([1,1,1,1,2,2,2,2]),
'cost' : pd.Series([10,20,30,40,50,60,70,80]),
'mth': pd.Series([3,3,4,5,3,4,4,5])}
cost id mth
0 10 1 3
1 20 1 3
2 30 1 4
3 40 1 5
4 50 2 3
5 60 2 4
6 70 2 4
7 80 2 5
Run Code Online (Sandbox Code Playgroud)
我可以得到每月的总和,但我想要每个user_id的平均月份.
df.groupby(['id','mth'])['cost'].sum()
id mth
1 3 30
4 30
5 40
2 3 50
4 130
5 80
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
id average_monthly
1 (30+30+40)/3
2 (50+130+80)/3
Run Code Online (Sandbox Code Playgroud) 下面是使用pandas pivot_table函数后以.csv格式输出的数据透视表的片段:
Sub-Product 11/1/12 11/2/12 11/3/12 11/4/12 11/5/12 11/6/12
GP Acquisitions 164 168 54 72 203 167
GP Applications 190 207 65 91 227 200
GPF Acquisitions 1124 1142 992 1053 1467 1198
GPF Applications 1391 1430 1269 1357 1855 1510
Run Code Online (Sandbox Code Playgroud)
我现在唯一需要做的就是在将每个子产品输出到.csv文件之前,在pandas中使用groupby来按周汇总值.
下面是我想要的输出,但它是在Excel中完成的.第一列可能不完全相同,但我很好.我需要做的主要事情是按周分组,这样我就能得到按周计算的数据总和.(查看顶行的日期如何按每7天分组一次).希望能够使用python/pandas来做到这一点.可能吗?
Row Labels 11/4/12 - 11/10/12 11/11/12 - 11/17/12
GP
Acquisitions 926 728
Applications 1092 889
GPF
Acquisitions 8206 6425
Applications 10527 8894
Run Code Online (Sandbox Code Playgroud) 我希望在loc函数中有2个条件但是&&或者and运算符似乎不起作用:
DF:
business_id ratings review_text
xyz 2 'very bad'
xyz 1 'passable'
xyz 3 'okay'
abc 2 'so so'
Run Code Online (Sandbox Code Playgroud)
mycode的:我试图收集所有review_text它的收视率< 3,并有id = xyz到一个列表
id = 'xyz'
mylist = df.loc[df['ratings'] < 3 and df[business_id] ==id,'review_text'].values.tolist()
Run Code Online (Sandbox Code Playgroud)
我应该得到:
['very bad','passable']
Run Code Online (Sandbox Code Playgroud)
这段代码不起作用,我得到错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Run Code Online (Sandbox Code Playgroud)
我如何and在这里正确使用操作员?
我想更快地运行gensim WMD相似性.通常,这是文档中的内容:示例语料库:
my_corpus = ["Human machine interface for lab abc computer applications",
>>> "A survey of user opinion of computer system response time",
>>> "The EPS user interface management system",
>>> "System and human system engineering testing of EPS",
>>> "Relation of user perceived response time to error measurement",
>>> "The generation of random binary unordered trees",
>>> "The intersection graph of paths in trees",
>>> "Graph minors IV Widths of trees and well quasi ordering",
>>> "Graph minors A …Run Code Online (Sandbox Code Playgroud) 我正在使用 python 的请求库从 API 执行“GET”。这是我的代码的一部分:
payload = { 'topicIds':'128487',
'topicIds':'128485',
'topicIds': '242793',
'timePeriod':'10d', }
r= requests.get(url, params=payload, headers=headers)
Run Code Online (Sandbox Code Playgroud)
根据 API 文档,我们可以为一个请求分配多个 topicId,如下所示: <url>topicId=123&topicId=246
当我尝试将 topicIds 值设置为这样的列表时:
payload = { 'topicIds':['128487' , '242793'],
Run Code Online (Sandbox Code Playgroud)
我收到一个错误: {u'error': u'topicIds: has 2 terms, should be between 0 and 1'}
但是,当我运行代码时,我只从最后一个 topicIds => 'topicIds': '242793' 获取数据我是否错误地编写了有效负载字典?
谢谢,
我正在尝试绘制频率分布(单词的出现次数及其频率)
这是我的代码:
import matplotlib.pyplot as plt
y = [1,2,3,4,5]
x = ['apple', 'orange', 'pear', 'mango', 'peach']
plt.bar(x,y)
plt.show
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
TypeError: cannot concatenate 'str' and 'float' objects
Run Code Online (Sandbox Code Playgroud) 我要在写入CSV文件时追加到新行。当前的CSV文件如下所示:
a,b,c
1,1,1
Run Code Online (Sandbox Code Playgroud)
我要附加到CSV文件的代码:
with open('mycsvfile.csv','a') as f:
writer=csv.writer(f)
writer.writerow(['0','0','0'])
Run Code Online (Sandbox Code Playgroud)
新的mycsvfile:
a,b,c
1,1,1,0,0,0
Run Code Online (Sandbox Code Playgroud)
我想要的是:
a,b,c
1,1,1
0,0,0
Run Code Online (Sandbox Code Playgroud) 当docker启动时,我有2个相互依赖的文件.1是一个烧瓶文件,一个是具有一些功能的文件.当docker启动时,只会执行函数文件,但它会从flask文件中导入flask变量.例:
Flaskfile
import flask
from flask import Flask, request
import json
_flask = Flask(__name__)
@_flask.route('/', methods = ['POST'])
def flask_main():
s = str(request.form['abc'])
ind = global_fn_main(param1,param2,param3)
return ind
def run(fn_main):
global global_fn_main
global_fn_main = fn_main
_flask.run(debug = False, port = 8080, host = '0.0.0.0', threaded = True)
Run Code Online (Sandbox Code Playgroud)
主文件
import flaskfile
#a few functions then
if__name__ == '__main__':
flaskfile.run(main_fn)
Run Code Online (Sandbox Code Playgroud)
脚本运行良好,无需枪支.
Dockerfile
FROM python-flask
ADD *.py *.pyc /code/
ADD requirements.txt /code/
WORKDIR /code
EXPOSE 8080
CMD ["python","main_file.py"]
Run Code Online (Sandbox Code Playgroud)
在命令行中:我通常会这样做:docker run -it -p …