小编Kev*_*vin的帖子

如何在matplotlib中显示所有标签值?

我有两个列表,当我使用以下代码绘图时,x轴最多只显示12(最大值为15).我可以知道如何将x列表中的所有值显示到x轴?提前致谢.

x = [4,5,6,7,8,9,10,11,12,13,14,15,0,1,2,3]
y = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(np.arange(len(x)), y, 'o')
ax1.set_xticklabels(x)
plt.show()
Run Code Online (Sandbox Code Playgroud)

如果我在set_xticklabels函数中设置minor = True ,它会显示所有x = 2,4,6,8,..,16 ......但我想要所有值.

PS我的x轴未排序,应显示如图所示.

python graph matplotlib axis-labels

18
推荐指数
1
解决办法
2万
查看次数

从Crypto导入Random - > ImportError:无法导入名称Random

我已经将pycrypto(版本2.3)安装到/usr/local/lib/python2.6/dist-packages/Crypto/,我可以在那里看到Random包.

但是当我尝试导入Crypto.Random时,它让我大吃一惊

from Crypto.Random import *
ImportError: No module named Random
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会发生这种情况?谢谢.

import Crypto
import os
print(Crypto.__file__);
print (dir(Crypto));
print(os.listdir(os.path.dirname(Crypto.__file__)))
Run Code Online (Sandbox Code Playgroud)

结果:

/usr/lib/python2.6/dist-packages/Crypto/__init__.pyc
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__revision__', '__version__']
['Hash', 'Protocol', 'PublicKey', 'test.py', 'Util', 'test.pyc', '__init__.pyc', '__init__.py', 'Cipher']
Run Code Online (Sandbox Code Playgroud)

python pycrypto

13
推荐指数
1
解决办法
4万
查看次数

如何在pandas数据帧中获取单元格值的长度?

有一个熊猫数据帧:

idx Event
0   abc/def
1   abc
2   abc/def/hij
Run Code Online (Sandbox Code Playgroud)

跑: df['EventItem'] = df['Event'].str.split("/")

拿到:

idx EventItem
0   ['abc','def']
1   ['abc']
2   ['abc','def','hij']
Run Code Online (Sandbox Code Playgroud)

想要获得每个的长度cell,运行df['EventCount'] = len(df['EventItem'])

拿到:

idx EventCount
0   6
1   6
2   6
Run Code Online (Sandbox Code Playgroud)

如何获得正确的计数如下?

idx EventCount
0   2
1   1
2   3
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

11
推荐指数
2
解决办法
2万
查看次数

如何在Ubuntu 11.10中使用Python 2.6?

Ubuntu 11.10上的默认Python版本是2.7,但我正在寻找2.6.如何将其设置为默认值以及可执行文件位于何处?

我键入which python2.6但它什么也没有返回,但我确实有一个python2.6文件夹/usr/lib/python2.6.但它看起来不像是在同一条路径上的python2.7 /usr/lib/.在python2.6中,有两个文件夹:dist-packageslib-dynload.

其实我正在配置PyDev,它要求我指定python2.6可执行文件的位置.python2.7可执行文件只需使用自动配置即可轻松定位,因为它是默认设置.

python ubuntu pydev

9
推荐指数
1
解决办法
1万
查看次数

$运算符对原子向量无效

我有一个非常简单的数据集,我试图在表的第一列执行table(),但R返回一个标题错误消息.我在网上搜索但不太明白为什么会发生这种情况,因为R接受我的桌子作为一张桌子......有人可以建议吗?

我的期望输出:

> table(tab$V1)
  CA 1
  CO 1
  OH 2
Run Code Online (Sandbox Code Playgroud)

然而它返回:

> tabraw
  V1        V2
1 OH Cleveland
2 OH  Columbus
3 CO    Denver
4 CA   SanFran

> tab <- table(tabraw)

> tab
    V2
V1   Cleveland Columbus Denver SanFran
  CA         0        0      0       1
  CO         0        0      1       0
  OH         1        1      0       0

> table(tab$V1)
Error in tab$V1 : $ operator is invalid for atomic vectors
Run Code Online (Sandbox Code Playgroud)

r

7
推荐指数
1
解决办法
8万
查看次数

R中层次聚类的奇怪错误

我的R程序如下:

hcluster <- function(dmatrix) {
    imatrix <- NULL
    hc <- hclust(dist(dmatrix), method="average")
    for(h in sort(unique(hc$height))) {
        hc.index <- c(h,as.vector(cutree(hc,h=h)))
        imatrix <- cbind(imatrix, hc.index)
    }
    return(imatrix)
}

dmatrix_file = commandArgs(trailingOnly = TRUE)[1]
print(paste('Reading distance matrix from', dmatrix_file))
dmatrix <- as.matrix(read.csv(dmatrix_file,header=FALSE))

imatrix <- hcluster(dmatrix)
imatrix_file = paste("results",dmatrix_file,sep="-")
print(paste('Wrinting results to', imatrix_file))
write.table(imatrix, file=imatrix_file, sep=",", quote=FALSE, row.names=FALSE, col.names=FALSE)
print('done!')
Run Code Online (Sandbox Code Playgroud)

我的输入是距离矩阵(当然是对称的).当我执行上面的程序时,距离矩阵大于大约数千条记录(几百条没有发生),它给了我错误信息:

Error in cutree(hc, h = h) : 
  the 'height' component of 'tree' is not sorted
(increasingly); consider applying as.hclust() first
Calls: hcluster -> …
Run Code Online (Sandbox Code Playgroud)

r cluster-analysis hierarchical-clustering

6
推荐指数
1
解决办法
1992
查看次数

如何使用Gson获取JSON元素类型?

在JSON文件中,文件中的每个对象都由不同类型的JSON元素组成.(整数,字符串,数组,对象数组等)

我的目标是列出所有元素名称和相应的类型.我可以知道如何在Gson中这样做吗?这样做的目的是创建Hive架构.

例:

{
  "number": 1, 
  "ts": "1386848002", 
  "cmpg": [
    {
      "id": 476, 
      "mcp": 0, 
      "deals": [ ], 
      "cookie": "uid:123", 
      "bid": [
        {
          "bId": 0, 
          "status": "ZB", 
          "rmtchID": -1
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

输出:

number int,
ts String,
cmpg array<map<String, Object>> // not sure how to interpret this...
Run Code Online (Sandbox Code Playgroud)

java arrays json hive gson

6
推荐指数
1
解决办法
1万
查看次数

将pandas dataframe中的列从String转换为Float

我已经阅读了各种解决方案,并尝试了此处所述的解决方案:Pandas:转换为数字,必要时创建NaN

但它并没有真正解决我的问题:我有一个包含多个列的数据框,其中一列['PricePerSeat_Outdoor']包含一些浮点值,一些空值和一些'-'

    print type(df_raw['PricePerSeat_Outdoor'][99])
    print df_raw['PricePerSeat_Outdoor'][95:101]
    df_raw['PricePerSeat_Outdoor'] = df_raw['PricePerSeat_Outdoor'].apply(pd.to_numeric, errors='coerce')
    print type(df_raw['PricePerSeat_Outdoor'][99]) 
Run Code Online (Sandbox Code Playgroud)

然后我得到了:

<type 'str'>
95     17.21
96     17.24
97         -
98         -
99      17.2
100    17.24
Name: PricePerSeat_Outdoor, dtype: object
<type 'str'>
Run Code Online (Sandbox Code Playgroud)

第98行和第99行的值未转换.同样,我已经尝试过多种方法,包括以下但是它没有用.非常感谢,如果有人能给我一些提示.

df_raw['PricePerSeat_Outdoor'] = df_raw['PricePerSeat_Outdoor'].apply(pd.to_numeric, errors='coerce')

另外,如何将多个列一次转换为数字?谢谢.

python typeconverter dataframe pandas

6
推荐指数
1
解决办法
2万
查看次数

Oozie String wf:errorCode(String node) 如何检查空?

我有一个名为“CW”的动作节点,之后我放置了一个决策节点来检查“CW”是否返回错误......我应该如何编写谓词?

我试过:

${ wf:errorCode('CW') eq '' } 然后去Y

${ wf:errorCode('CW') != '' } 然后去N

虽然它返回空字符串(没有错误),但它总是转到N. 有什么建议吗?谢谢!!

hadoop oozie

4
推荐指数
1
解决办法
3290
查看次数

Pandas round不适用于DataFrame

Round适用于单个元素,但不适用于DataFrame,尝试使用DataFrame.round()但不起作用......任何想法?谢谢.

有以下代码:

print "Panda Version: ", pd.__version__
print "['5am'][0]: ", x3['5am'][0]
print "Round element: ", np.round(x3['5am'][0]*4) /4
print "Round Dataframe: \r\n", np.round(x3 * 4, decimals=2) / 4
df = np.round(x3 * 4, decimals=2) / 4
print "Round Dataframe Again: \r\n", df.round(2)
Run Code Online (Sandbox Code Playgroud)

得到的结果:

Panda Version:  0.18.0
['5am'][0]:  0.279914529915
Round element:  0.25
Round Dataframe:
                 5am       6am      7am      8am      9am     10am     11am
Date
2016-07-11  0.279915  0.279915  2.85256  4.52778  6.23291  9.01496  8.53632
2016-07-12  0.339744  0.369658  2.67308  4.52778  5.00641  7.30983  6.98077
2016-07-13  0.399573  0.459402  2.61325 …
Run Code Online (Sandbox Code Playgroud)

python rounding dataframe pandas

4
推荐指数
3
解决办法
9703
查看次数