小编nit*_*tin的帖子

在Pandas DataFrame对象中重新定义索引

我试图重新索引一个pandas DataFrame对象,就像这样,

From:
            a   b   c
        0   1   2   3
        1  10  11  12
        2  20  21  22

To :
           b   c
       1   2   3
      10  11  12
      20  21  22
Run Code Online (Sandbox Code Playgroud)

我正在解决这个问题,如下所示,我得到了错误的答案.关于如何做到这一点的任何线索?

>>> col = ['a','b','c']
>>> data = DataFrame([[1,2,3],[10,11,12],[20,21,22]],columns=col)
>>> data
    a   b   c
0   1   2   3
1  10  11  12
2  20  21  22
>>> idx2 = data.a.values
>>> idx2
array([ 1, 10, 20], dtype=int64)
>>> data2 = DataFrame(data,index=idx2,columns=col[1:])
>>> data2
     b   c
1   11  12 …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

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

Pandas DataFrame上的条件逻辑

如何将条件逻辑应用于Pandas DataFrame.

请参见下面显示的DataFrame,

   data desired_output
0     1          False
1     2          False
2     3           True
3     4           True
Run Code Online (Sandbox Code Playgroud)

我的原始数据显示在"数据"列中,而next_output显示在其旁边.如果'data'中的数字低于2.5,则desired_output为False.

我可以应用一个循环并重新构建DataFrame ......但这将是'非pythonic'

python pandas

27
推荐指数
4
解决办法
10万
查看次数

在熊猫中创造百分位桶

我试图根据其值在百分位桶中对我的数据进行分类.我的数据看起来像,

a = pnd.DataFrame(index = ['a','b','c','d','e','f','g','h','i','j'], columns=['data'])
a.data = np.random.randn(10)
print a
print '\nthese are ranked as shown'
print a.rank()

       data
a -0.310188
b -0.191582
c  0.860467
d -0.458017
e  0.858653
f -1.640166
g -1.969908
h  0.649781
i  0.218000
j  1.887577

these are ranked as shown
   data
a     4
b     5
c     9
d     3
e     8
f     2
g     1
h     7
i     6
j    10
Run Code Online (Sandbox Code Playgroud)

为了对这些数据进行排名,我使用的是排名功能.但是,我有兴趣创造一个前20%的桶.在上面显示的示例中,这将是包含标签['c','j']的列表

desired result : ['c','j']
Run Code Online (Sandbox Code Playgroud)

我如何获得所需的结果

python pandas

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

在DataFrame索引中查找标签位置

我有一个pandas数据帧:

import pandas as pnd
d = pnd.Timestamp('2013-01-01 16:00')
dates = pnd.bdate_range(start=d, end = d+pnd.DateOffset(days=10), normalize = False)

df = pnd.DataFrame(index=dates, columns=['a'])
df['a'] = 6

print(df)
                     a
2013-01-01 16:00:00  6
2013-01-02 16:00:00  6
2013-01-03 16:00:00  6
2013-01-04 16:00:00  6
2013-01-07 16:00:00  6
2013-01-08 16:00:00  6
2013-01-09 16:00:00  6
2013-01-10 16:00:00  6
2013-01-11 16:00:00  6
Run Code Online (Sandbox Code Playgroud)

我有兴趣找到其中一个标签的标签位置,比方说,

ds = pnd.Timestamp('2013-01-02 16:00')
Run Code Online (Sandbox Code Playgroud)

看一下索引值,我知道这是这个标签的整数位置1.怎样才能让pandas告诉这个标签的整数值是多少?

python pandas

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

在DataFrame对象上使用rolling_apply

我正在尝试以滚动方式计算交易量加权平均价格.

要做到这一点,我有一个函数vwap为我这样做,像这样:

def vwap(bars):
    return ((bars.Close*bars.Volume).sum()/bars.Volume.sum()).round(2)
Run Code Online (Sandbox Code Playgroud)

当我尝试将此函数与rolling_apply一起使用时,如图所示,我收到一个错误:

import pandas.io.data as web
bars = web.DataReader('AAPL','yahoo')
print pandas.rolling_apply(bars,30,vwap)

AttributeError: 'numpy.ndarray' object has no attribute 'Close'
Run Code Online (Sandbox Code Playgroud)

这个错误对我有意义,因为rolling_apply不需要DataSeries或ndarray作为输入而不是dataFrame ..我正在这样做.

有没有办法使用rolling_apply到DataFrame来解决我的问题?

python pandas

10
推荐指数
1
解决办法
7901
查看次数

Pandas DataFrame和Series的矩阵乘法

我想做一个pandas数据帧和一系列的矩阵乘法

df = pandas.DataFrame({'a':[4,1,3], 'b':[5,2,4]},index=[1,2,3])
ser = pandas.Series([0.6,0.4])
Run Code Online (Sandbox Code Playgroud)

df是,

 a  b
1  4  5
2  1  2
3  3  4
Run Code Online (Sandbox Code Playgroud)

ser是,

0    0.6
1    0.4
Run Code Online (Sandbox Code Playgroud)

我想要的结果是矩阵产品,就像这样

ans是,

我可以通过使用numpy点运算符和重建我的dataFrame来做到这一点

c = a.values.dot(b.transpose())
c = pandas.DataFrame(c, index = a.index, columns = ['ans'])
print c


   ans
1  4.4
2  1.4
3  3.4
Run Code Online (Sandbox Code Playgroud)

在熊猫中有一个本地方法来做到这一点吗?

python matrix matrix-multiplication dot-product pandas

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

检查列表列表是否具有相同大小的列表

我需要验证我的列表列表是否在python中具有相同大小的列表

myList1 = [ [1,1] , [1,1]] // This should pass. It has two lists.. both of length 2
myList2 = [ [1,1,1] , [1,1,1], [1,1,1]] // This should pass, It has three lists.. all of length 3
myList3 = [ [1,1] , [1,1], [1,1]] // This should pass, It has three lists.. all of length 2
myList4 = [ [1,1,] , [1,1,1], [1,1,1]] // This should FAIL. It has three list.. one of which is different that the other …
Run Code Online (Sandbox Code Playgroud)

python list

7
推荐指数
2
解决办法
5109
查看次数

Python字符串对象不可调用

我无法理解为什么我得到以下语句的类型错误

log.debug('vec : %s blasted : %s\n' %(str(vec), str(bitBlasted)))

type(vec)  is unicode
bitBlasted is a list
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)

python string

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

跳过生成器函数中的值

我正在编写一个生成器函数,它给我字母字符,就像这样,

def gen_alphaLabels():
    a = range(65,91)
    for i in a:
        yield chr(i)

k = gen_alphaLabels()
for i in range(26):
    print k.next(),
Run Code Online (Sandbox Code Playgroud)

这会产生,

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Run Code Online (Sandbox Code Playgroud)

这有效......

我希望函数跳过donotInclude列表中的一些字符.我可以这样做是在发电机外面,像这样,

k = gen_alphaLabels()
donotInclude = ['D','K','J']
for i in range(26):
    r = k.next()
    if r not in donotInclude:
        print r,
Run Code Online (Sandbox Code Playgroud)

这产生了跳过'D','K'和'J'的理想结果

A B C E F G H I L …
Run Code Online (Sandbox Code Playgroud)

python generator

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

Python中的机器人运动

我正在尝试编程一个机器人来移动.机器人根据当前的位置移动.有四个地方可以:

LOCATION1 Motion Plan is like so,
5 6
3 4
1 2
Initial positon is (x1,y1)
This gets coded as (x1,y1)->(x1+dx,y1)->(x1,y1+dy)->(x1+dx,y1+dy) ... and so on

LOCATION2 Motion Plan is like so,
5 3 1
6 4 2
The initial position is (x1,y1)
This gets coded as (x1,y1)->(x1,y1-dy)->(x1-dx,y1)->(x1-dx,y1-dy) ... and so on

LOCATION3 Motion Plan is like so,
6 5
4 3
2 1
Initial positon is (x1,y1)
This gets coded as (x1,y1)->(x1-dx,y1)->(x1,y1+dy)->(x1-dx,y1+dy) ... and so on

LOCATION4 Motion Plan …
Run Code Online (Sandbox Code Playgroud)

python robotics

5
推荐指数
1
解决办法
842
查看次数