我有一个看起来像的df:
df.head()
Out[1]:
A B C
city0 40 12 73
city1 65 56 10
city2 77 58 71
city3 89 53 49
city4 33 98 90
Run Code Online (Sandbox Code Playgroud)
可以通过以下代码创建示例df:
df = pd.DataFrame(np.random.randint(100,size=(1000000,3)), columns=list('ABC'))
indx = ['city'+str(x) for x in range(0,1000000)]
df.index = indx
Run Code Online (Sandbox Code Playgroud)
我想做的是:
a)确定A列的适当直方图桶长度,并将每个城市分配给A列的桶
b)确定B列的适当直方图桶长度,并将每个城市分配给B列的桶
也许结果df看起来像(或者是否有更好的内置方式在熊猫?)
df.head()
Out[1]:
A B C Abkt Bbkt
city0 40 12 73 2 1
city1 65 56 10 4 3
city2 77 58 71 4 3
city3 89 53 49 5 3
city4 33 98 90 …Run Code Online (Sandbox Code Playgroud) 对于每对src和dest机场城市,我想在a给定列值的情况下返回列的百分位数b.
我可以手动执行此操作:
示例df只有2对src/dest(我的实际df中有数千个):
dt src dest a b
0 2016-01-01 YYZ SFO 548.12 279.28
1 2016-01-01 DFW PDX 111.35 -65.50
2 2016-02-01 YYZ SFO 64.84 342.35
3 2016-02-01 DFW PDX 63.81 61.64
4 2016-03-01 YYZ SFO 614.29 262.83
{'a': {0: 548.12,
1: 111.34999999999999,
2: 64.840000000000003,
3: 63.810000000000002,
4: 614.28999999999996,
5: -207.49000000000001,
6: 151.31999999999999,
7: -56.43,
8: 611.37,
9: -296.62,
10: 6417.5699999999997,
11: -376.25999999999999,
12: 465.12,
13: -821.73000000000002,
14: 1270.6700000000001,
15: -1410.0899999999999,
16: …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的数据框:
import pandas as pd
datelisttemp = pd.date_range('1/1/2014', periods=3, freq='D')
s = list(datelisttemp)*3
s.sort()
df = pd.DataFrame({'BORDER':['GERMANY','FRANCE','ITALY','GERMANY','FRANCE','ITALY','GERMANY','FRANCE','ITALY' ], 'HOUR1':[2 ,2 ,2 ,4 ,4 ,4 ,6 ,6, 6],'HOUR2':[3 ,3 ,3, 5 ,5 ,5, 7, 7, 7], 'HOUR3':[8 ,8 ,8, 12 ,12 ,12, 99, 99, 99]}, index=s)
Run Code Online (Sandbox Code Playgroud)
这给了我:
Out[458]: df
BORDER HOUR1 HOUR2 HOUR3
2014-01-01 GERMANY 2 3 8
2014-01-01 FRANCE 2 3 8
2014-01-01 ITALY 2 3 8
2014-01-02 GERMANY 4 5 12
2014-01-02 FRANCE 4 5 12
2014-01-02 ITALY 4 …Run Code Online (Sandbox Code Playgroud) 我有一个csv看起来像(headers =第一行):
name,a,a1,b,b1
arnold,300311,arnld01,300311,arnld01
sam,300713,sam01,300713,sam01
Run Code Online (Sandbox Code Playgroud)
当我跑:
df = pd.read_csv('file.csv')
Run Code Online (Sandbox Code Playgroud)
列a和b有一个.0连接到像这样的结尾:
df.head()
name,a,a1,b,b1
arnold,300311.0,arnld01,300311.0,arnld01
sam,300713.0,sam01,300713.0,sam01
Run Code Online (Sandbox Code Playgroud)
列a和b整数或空格,所以为什么pd.read_csv()像浮点数一样对待它们?如何确保它们在读取时是整数?
有谁知道如何在plotly内部jupyter notebook使用python?
文档组织得不是很好,至少不是我的观点.
例如,我可以运行以下代码,但它会在HTML文件中生成一个可以在外部访问的图形jupyter notebook.有没有办法在笔记本内部渲染图形?
我不清楚(因为文档很差)是否需要凭证才能plotly用于内部的情节jupyter notebook?是否仅需要凭据才能在云上托管图表而已?
我也发现没有任何真实的文档cufflinks.所有它说是它使得使用plotly与pandasdataframes容易.但是对于那些不是开发人员的人来说,如果有详细的文档说明为什么它是必要的以及它究竟是做什么使生活更轻松将会更好.
import plotly.plotly as py
from plotly.graph_objs import *
trace0 = Scatter(
x=[1, 2, 3, 4],
y=[10, 15, 13, 17]
)
trace1 = Scatter(
x=[1, 2, 3, 4],
y=[16, 5, 11, 9]
)
data = Data([trace0, trace1])
plotly.offline.plot(data, filename = 'basic-line')
/Users/blahblah/anaconda/lib/python2.7/site-packages/plotly/offline/offline.py:433: UserWarning:
Your filename `basic-line` didn't end with .html. Adding .html to the end of your …Run Code Online (Sandbox Code Playgroud) 我在MacOS 10.6.4上使用PyCharm(1.5.4)作为我的python IDE.我正在修改一些代码来操纵股票价格数据.作为其中的一部分,我想通过使用Pandas 0.6.0附带的DataReader函数从yahoo导入价格数据.代码如下:
http://www.statalgo.com/2011/09/08/pandas-getting-financial-data-from-yahoo-fred-etc/
from pandas import ols, DataFrame
from pandas.stats.moments import rolling_std
from pandas.io.data import DataReader
import datetime
sp500 = DataReader("^GSPC", "yahoo", start=datetime.datetime(1990, 1, 1))
sp500_returns = sp500["adj clos"].shift(-250)/sp500["adj clos"] - 1
gdp = DataReader("GDP", "fred", start=datetime.datetime(1990, 1, 1))["value"]
gdp_returns = (gdp/gdp.shift(1) - 1)
gdp_std = rolling_std(gdp_returns, 10)
gdp_standard = gdp_returns / gdp_std
gdp_on_sp = ols(y=sp500_returns, x=DataFrame({"gdp": gdp_standard}))
sp500.plot()
gdp.plot()
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我收到以下错误:
Traceback (most recent call last):
File "/Users/MyName/PycharmProjects/test/mytest", line 3, in <module>
from pandas.io.data import DataReader
ImportError: No …Run Code Online (Sandbox Code Playgroud) 我有2张桌子:
表1的示例:
StationID1 StationID2 Name1 Name2 Lattitude1 Longitude1 Lattitude2 Longitude2 Distance
------------------------------------------------------------------------------------------------
93353477 52452 FOO BAR NULL NULL NULL NULL NULL
93353527 52452 HENRY BENNY NULL NULL NULL NULL NULL
93353551 52452 GALE SAM NULL NULL NULL NULL NULL
Run Code Online (Sandbox Code Playgroud)
表2的示例:
IDInfo Name Lattitude Longitude
-------------------------------------------
93353477 BAR 37.929654 -87.029622
Run Code Online (Sandbox Code Playgroud)
我想用驻留的坐标信息更新此表tableA.我尝试按照SQL Server 2005执行以下操作:无法绑定多部分标识符....
update table1
set t1.[Lattitude1] = t2.[Lattitude]
from table1 t1
left join table2 t2
on (t1.StationID1 = …Run Code Online (Sandbox Code Playgroud) DF1:
City, 2015-12-31, 2016-01-31, ...
YYZ 562.14, -701.18, ...
DFW 562.14, -701.18, ...
YYC 562.14, -701.18, ...
Run Code Online (Sandbox Code Playgroud)
DF2:
City, 2015-12-31, 2016-01-31, ...
SFO 562.14, -701.18, ...
PDX 562.14, -701.18, ...
LAX 562.14, -701.18, ...
Run Code Online (Sandbox Code Playgroud)
我想从df2中减去df1.即减去各个日期列中的值.
我尝试了以下方法:
df2.subtract(df1, fill_value=0)
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
TypeError: unsupported operand type(s) for -: 'str' and 'float'
Run Code Online (Sandbox Code Playgroud)
我认为错误是因为操作无法理解如何在City列中减去字符串,这显然是有道理的,因为减去Cities是荒谬的.
这篇文章[链接]中接受的答案似乎表明这是可能的.我是这个问题的作者,但现在似乎无法让它发挥作用.
问题:通过作为列表传入的分隔符将字符串拆分为单词列表.
串: "After the flood ... all the colors came out."
期望的输出: ['After', 'the', 'flood', 'all', 'the', 'colors', 'came', 'out']
我写了以下函数 - 注意我知道有更好的方法来使用函数内置的一些pythons来分割字符串但是为了学习我想我会这样做:
def split_string(source,splitlist):
result = []
for e in source:
if e in splitlist:
end = source.find(e)
result.append(source[0:end])
tmp = source[end+1:]
for f in tmp:
if f not in splitlist:
start = tmp.find(f)
break
source = tmp[start:]
return result
out = split_string("After the flood ... all the colors came out.", " .")
print out
['After', 'the', 'flood', 'all', …Run Code Online (Sandbox Code Playgroud) 关于这个主题有很多SO帖子,但大多数是针对Windows并使用GUI.所有其他帖子似乎都使用CLI.
在MacOS/Linux上是否有puttygen的GUI版本,如果是这样的说明如何安装/使用?CLI似乎给我一个错误.
Usage: puttygen ( keyfile | -t type [ -b bits ] )
[ -C comment ] [ -P ] [ -q ]
[ -o output-keyfile ] [ -O type | -l | -L | -p ]
>puttygen mykey.pem -t rsa -b 2048 -o mykey.ppk
puttygen: cannot both load and generate a key
Run Code Online (Sandbox Code Playgroud)
我的命令有问题吗?
python ×7
pandas ×6
apply ×1
bokeh ×1
csv ×1
importerror ×1
integer ×1
key-pair ×1
linux ×1
module ×1
pem ×1
percentile ×1
pivot ×1
plot ×1
plotly ×1
private-key ×1
putty ×1
pycharm ×1
scipy ×1
seaborn ×1
split ×1
sql ×1
sql-server ×1
sql-update ×1
stack ×1
string ×1
subtraction ×1