我有一个pandas数据框,如下所示:
[('1975801_m', 1 0.203244
10 -0.159756
16 -0.172756
19 -0.089756
20 -0.033756
23 -0.011756
24 0.177244
32 0.138244
35 -0.104756
36 0.157244
40 0.108244
41 0.032244
42 0.063244
45 0.362244
59 -0.093756
62 -0.070756
65 -0.030756
66 -0.100756
73 -0.140756
77 -0.110756
81 -0.100756
84 -0.090756
86 -0.180756
87 0.119244
88 0.709244
102 -0.030756
105 -0.000756
107 -0.010756
109 0.039244
111 0.059244
Name: RTdiff), ('3878418_m', 1637 0.13811
1638 -0.21489
1644 -0.15989
1657 -0.11189
1662 -0.03289
1666 -0.09489
1669 0.03411
1675 …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种方法可以在 Python 中使用 matplotlib 绘制直方图和尖顶图。
我有以下用于绘制直方图的内容
a = np.array(values)
plt.hist(a, 32, normed=0, facecolor='blue', alpha = 0.25)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但我不知道 matplotlib 是否有绘制尖顶图的好方法。
这就是我正在做的:
a = np.array(values)
bins = np.arange(int(min), int(max) + 2)
histogram = np.histogram(a, bins = bins, normed = True)
v = []
s = 0.0
for e in histogram[0]:
s = s + e
v.append(s)
v[0] = histogram[0][0]
plt.plot(v)
plt.show()
Run Code Online (Sandbox Code Playgroud) 我有一个像这样加载的DataFrame
minData = pd.read_csv(
currentSymbol["fullpath"],
header = None,
names = ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Split Factor', 'Earnings', 'Dividends'],
parse_dates = [["Date", "Time"]],
date_parser = lambda x : datetime.datetime.strptime(x, '%Y%m%d %H%M'),
index_col = "Date_Time",
sep=' ')
Run Code Online (Sandbox Code Playgroud)
数据看起来像这样
>>> minData.index
<class 'pandas.tseries.index.DatetimeIndex'>
[1998-01-02 09:30:00, ..., 2013-12-09 16:00:00]
Length: 1373036, Freq: None, Timezone: None
>>>
>>> minData.head(5)
Open High Low Close Volume \
Date_Time
1998-01-02 09:30:00 8.70630 8.70630 8.70630 8.70630 420.73
1998-01-02 09:35:00 8.82514 8.82514 8.82514 8.82514 420.73
1998-01-02 09:42:00 …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Netezza 导出一个大文件(使用 Netezza ODBC + pyodbc),此解决方案会引发 memoryError,如果我在没有“list”的情况下循环,它会非常慢。你知道一个不会杀死我的服务器/python进程但可以运行得更快的中间解决方案吗?
cursorNZ.execute(sql)
archi = open("c:\test.csv", "w")
lista = list(cursorNZ.fetchall())
for fila in lista:
registro = ''
for campo in fila:
campo = str(campo)
registro = registro+str(campo)+";"
registro = registro[:-1]
registro = registro.replace('None','NULL')
registro = registro.replace("'NULL'","NULL")
archi.write(registro+"\n")
Run Code Online (Sandbox Code Playgroud)
- - 编辑 - -
谢谢,我正在尝试:其中“sql”是查询,cursorNZ 是
connMy = pyodbc.connect(DRIVER=.....)
cursorNZ = connNZ.cursor()
chunk = 10 ** 5 # tweak this
chunks = pandas.read_sql(sql, cursorNZ, chunksize=chunk)
with open('C:/test.csv', 'a') as output:
for n, df in enumerate(chunks):
write_header …Run Code Online (Sandbox Code Playgroud) 我正在使用64位无符号整数,并在比特移位后比较该值,然后解码其余的位值.我正在迭代数百万个值并尝试最小化处理时间.
问题是uint64和numpy-uint64都不支持位移.我试图避免使用int64来避免负值.
示例数据:移位后的0x8204000000000080(字>> 60):= - 8#,但与0x8相比
循环一百万次,看看它需要多长时间,发现在所有方法中,'>>'移位运算符是最有效的,具有调用abs()函数的下一个最佳选项.对此有更好的解决方案吗?
循环代码:
import numpy as np
import time
start_time= time.time()
for i in range(1000000):
x= np.int64(-1)
x=np.right_shift(x,60)
print (time.time()-start_time)
start_time= time.time()
for i in range(1000000):
x= np.uint64(-1)
x=int(x/(2**60))
print (time.time()-start_time)
start_time= time.time()
for i in range(1000000):
x= np.int64(-1)
x=abs(x>>60)
print (time.time()-start_time)
start_time= time.time()
for i in range(1000000):
x= np.int64(-1)
x= x>>60
print (time.time()-start_time)
Run Code Online (Sandbox Code Playgroud)
输出:
2.055999994277954
3.1540000438690186
0.619999885559082
0.5810000896453857
Run Code Online (Sandbox Code Playgroud) 我的目的是在seaborn的特定坐标处添加补丁lmplot:
有没有办法添加一个矩形/正方形补丁lmplot?
我能够通过 打印出情节sns.lmplot()。但是当我尝试使用ax.add_patch()具有相关坐标的语句添加矩形面片时,出现错误。
#Sample code to generate lmplot and add patch
ax= sns.lmplot('A', 'B', hue="group", data=res_me,fit_reg=False, \
palette="Set1",size=10, aspect=1, scatter_kws={"s": 100,"linewidths":2,"edgecolor":"black"})
ax.add_patch(patches.Rectangle((0.912, 0.72), 1.02, .802,fill=False,edgecolor='green',lw=3))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误。
AttributeError: 'FacetGrid' object has no attribute 'add_patch'
Run Code Online (Sandbox Code Playgroud)
那么我们可以添加补丁吗FacetGrid?
for如果i等于2的倍数,如何在循环中打印换行符?
例如:
for i in range(0, 10):
for j in range(0, 10):
print('Hello')
##Print a newline when i equals to 2, 4, 6, 8, and 10
##if(i == 2):
##print('\n')
Run Code Online (Sandbox Code Playgroud) 我正在运行 Python 3.7.4,并且在处理某些事情时发现了一些不良行为,然后我将其简化为:
>>> x = 5
>>> x -= 1 if False else print("blah")
blah
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -=: 'int' and 'NoneType'
Run Code Online (Sandbox Code Playgroud)
除非有什么明显的东西我只是想念?为什么它甚至试图评估 -= 如果它落入其他?
我有一个格式如下的元组值列表:
[{'name': 'name', 'value': 'Zach'},
{'name': 'email', 'value': 'zach@gmail.com'},
{'name': 'age', 'value': '21'}]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用正确的键/值排列字典对象。这是到目前为止我已经能够想出的代码:
msg_dict = dict((k, v) for k, v in msg_ls)
print(msg_dict)
Run Code Online (Sandbox Code Playgroud)
这是打印
{'name': 'value'}
{'name': 'value'}
{'name': 'value'}
Run Code Online (Sandbox Code Playgroud)
虽然我需要它阅读
'name': Zach
'email': zach@gmail.com
'age': 21
Run Code Online (Sandbox Code Playgroud) python ×9
pandas ×2
plot ×2
boxplot ×1
csv ×1
dataframe ×1
dictionary ×1
for-loop ×1
matplotlib ×1
netezza ×1
numpy ×1
seaborn ×1
statistics ×1
tuples ×1