我目前正在使用Anaconda的Spyder,我正在尝试将包含float类型的数组转换为int类型:
x = np.array([1, 2, 2.5])
x.astype(int)
print x
Run Code Online (Sandbox Code Playgroud)
结果仍未改变:
[1. 2. 2.5]
Run Code Online (Sandbox Code Playgroud)
思考?
我想在Python中的函数参数中传递一个公式,其中公式是其他函数参数的组合.原则上,这将是这样的:
myfunction(x=2,y=2,z=1,formula="x+2*y/z")
6
Run Code Online (Sandbox Code Playgroud)
或更具体:
def myformula(x,y,z,formula):
return formula(x,y,z)
Run Code Online (Sandbox Code Playgroud)
这将允许用户根据x,y和z选择任何算术表达式,而无需创建新函数.
我预见的一种可能性是在函数内的代码行中转换字符串.在Python中有什么可能吗?还是其他任何想法?谢谢
我一直在尝试使用pandas.to_datetime在我的代码库中的时间戳格式之间进行转换,但是当提供字符串输入时,有时pandas似乎无法正确提取 UTC 偏移量:
这是一个正确的转换,UTC 偏移量被正确捕获,反映在 Timestamp 对象中:
In[76]: pd.to_datetime('2014-04-09T15:29:59.999993-0500', utc=True)
Out[76]: Timestamp('2014-04-09 20:29:59.999993+0000', tz='UTC')
Run Code Online (Sandbox Code Playgroud)
这是一个替代字符串表示,它仍然是有效的ISO 8601日期时间字符串,但 UTC 偏移量-0500似乎被忽略:
In[77]: pd.to_datetime('2014-04-09T152959.999993-0500', utc=True)
Out[77]: Timestamp('2014-04-09 15:29:59.999993+0000', tz='UTC')
Run Code Online (Sandbox Code Playgroud)
另一方面,dateutil包可以很好地处理事情:
In[78]: dateutil.parser.parse('2014-04-09T152959.999993-0500')
Out[78]: datetime.datetime(2014, 4, 9, 15, 29, 59, 999993, tzinfo=tzoffset(None, -18000))
Run Code Online (Sandbox Code Playgroud)
我当然可以使用,dateutil但是否有某种原因pandas.to_datetime不能正确处理不同的 ISO 日期时间字符串。我在这里做错了吗?
使用 Python 2.7.6 和 pandas 0.13.1
如何在python中删除以特定字符开头的单词?
例如。
string = 'Hello all please help #me'
Run Code Online (Sandbox Code Playgroud)
我想删除以 #
我想要的结果是:
Hello all please help
Run Code Online (Sandbox Code Playgroud) 我有2个DataFrames:
city count school
0 New York 1 school_3
1 Washington 1 School_4
2 Washington 1 School_5
3 LA 1 School_1
4 LA 1 School_4
city count school
0 New York 1 School_3
1 Washington 1 School_1
2 LA 1 School_3
3 LA 2 School_4
Run Code Online (Sandbox Code Playgroud)
我想得到这个结果:
city count school
0 New York 2 school_3
1 Washington 1 School_1
2 Washington 1 School_4
3 Washington 1 School_5
4 LA 1 School_1
5 LA 1 School_3
6 LA 3 School_4
Run Code Online (Sandbox Code Playgroud)
以下是代码.
d1 …Run Code Online (Sandbox Code Playgroud) 我正在努力正确加载一个带有空格的多行标题的csv.CSV看起来像这样:
,,C,,,D,,
A,B,X,Y,Z,X,Y,Z
1,2,3,4,5,6,7,8
Run Code Online (Sandbox Code Playgroud)

我想得到的是:

当我尝试加载时pd.read_csv(file, header=[0,1], sep=','),我最终得到以下内容:

有没有办法获得理想的结果?
注意:或者,我会接受这样的结果:

使用的版本:
我想得到以下日期的平均值.我考虑过将所有数据转换为秒,然后对它们求平均值.但是可能有更好的方法.
date = ['2016-02-23 09:36:26', '2016-02-24 10:00:32', '2016-02-24 11:28:22', '2016-02-24 11:27:20', '2016-02-24 11:24:15', '2016-02-24 11:20:25', '2016-02-24 11:17:43', '2016-02-24 11:12:03', '2016-02-24 11:09:11', '2016-02-24 11:08:44', '2016-02-24 11:05:28', '2016-02-24 11:03:23', '2016-02-24 10:58:08', '2016-02-24 10:53:59', '2016-02-24 10:49:34', '2016-02-24 10:43:33', '2016-02-24 10:35:27', '2016-02-24 10:31:50', '2016-02-24 10:31:17', '2016-02-24 10:30:05', '2016-02-24 10:29:21']
Run Code Online (Sandbox Code Playgroud)
讨厌的解决方案:
import datetime
import time
import numpy as np
date = ['2016-02-23 09:36:26', '2016-02-24 10:00:32', '2016-02-24 11:28:22', '2016-02-24 11:27:20', '2016-02-24 11:24:15', '2016-02-24 11:20:25', '2016-02-24 11:17:43', '2016-02-24 11:12:03', '2016-02-24 11:09:11', '2016-02-24 11:08:44', '2016-02-24 11:05:28', '2016-02-24 11:03:23', …Run Code Online (Sandbox Code Playgroud) 我在result.csv文件中有以下数据,我需要绘制成线图.
ColA ColB
93 46
94 56
95 66
97 76
100 86
103 96
110 106
Run Code Online (Sandbox Code Playgroud)
我拥有的是什么
from numpy import genfromtxt
import matplotlib.pyplot as plt
per_data=genfromtxt('result.csv',delimiter=','
plt.xlabel ('x stuff')
plt.ylabel ('y stuff')
plt.title('my test result')
plt.show()
Run Code Online (Sandbox Code Playgroud)
如何将每列数据输入图表并查看其趋势?每列因新数据而每天都会增长的大小.
我想在python中找到两个向量之间的顺时针角度,角度应该在(-90,90)范围内
计算角度的方程/代码是什么?
class Vect:
def __init__(self, a, b):
self.a = a
self.b = b
def findClockwiseAngle(self, other):
## how to compute ??
pass
vector1 = Vect(a1,b1) ## a1*i + b1*j
vector2 = Vect(a2,b2) ## a2*i + b2*j
angle = vect1.findClockwiseAngle(vect2)
Run Code Online (Sandbox Code Playgroud) 试图在Ubuntu 14.04上翻译pypy3.我按照这里的说明操作:http://pypy.readthedocs.org/en/latest/getting-started-python.html#installation,但是我收到了一个错误.
pypy ../../rpython/bin/rpython -O2 --sandbox targetpypystandalone.py gives me
File "../../rpython/bin/rpython", line 17
print __doc__
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
查看文件rpython/bin/rpython,我看到一个if语句,我似乎正在点击
if len(sys.argv) == 1:
print __doc__
sys.exit(1)
Run Code Online (Sandbox Code Playgroud)
我没有通过哪些论据我应该是谁?
python ×9
pandas ×4
arrays ×2
datetime ×2
numpy ×2
csv ×1
function ×1
graph ×1
line ×1
matplotlib ×1
pypy ×1
python-2.7 ×1
python-3.x ×1
types ×1