簡體   English   中英

根據行條件從熊貓數據框中選擇列

[英]Selecting columns from a pandas dataframe based on row conditions

我有一個熊貓數據框

In [1]: df = DataFrame(np.random.randn(10, 4))

有沒有一種方法我只能選擇(最后一行)值> 0的列,所需的輸出將是一個新的數據框,其中所有行都與最后一行> 0的列相關聯

您可以使用從條件生成的布爾系列來索引感興趣的列:

In [30]:

df = pd.DataFrame(np.random.randn(10, 4))
df
Out[30]:
          0         1         2         3
0 -0.667736 -0.744761  0.401677 -1.286372
1  1.098134 -1.327454  1.409357 -0.180265
2 -0.105780  0.446195 -0.562578 -0.746083
3  1.366714 -0.685103  0.982354  1.928026
4  0.091040 -0.689676  0.425042  0.723466
5  0.798305 -1.454922 -0.017695  0.515961
6 -0.786693  1.496968 -0.112125 -1.303714
7 -0.211216 -1.321854 -0.892023 -0.583492
8  1.293255  0.936271  1.873870  0.790086
9 -0.699665 -0.953611  0.139986 -0.200499
In [32]:

df[df.columns[df.iloc[-1]>0]]
Out[32]:
          2
0  0.401677
1  1.409357
2 -0.562578
3  0.982354
4  0.425042
5 -0.017695
6 -0.112125
7 -0.892023
8  1.873870
9  0.139986

查看pandasql: https ://pypi.python.org/pypi/pandasql

這篇博客文章是有關將SQL用於Pandas DataFrames的出色教程: http ://blog.yhathq.com/posts/pandasql-sql-for-pandas-dataframes.html

這應該使您開始:

from pandasql import *
import pandas

def pysqldf(q):
    return sqldf(q, globals())

q = """ 
    SELECT
        *
    FROM 
        df

    WHERE
        value > 0
    ORDER BY 1; 
"""

df = pysqldf(q)
In [201]: df = pd.DataFrame(np.random.randn(10, 4))

In [202]: df
Out[202]: 
          0         1         2         3
0 -1.380064  0.391358 -0.043390 -1.970113
1 -0.612594 -0.890354 -0.349894 -0.848067
2  1.178626  1.798316  0.691760  0.736255
3 -0.909491  0.429237  0.766065 -0.605075
4 -1.214366  1.907580 -0.583695  0.192488
5 -0.283786 -1.315771  0.046579 -0.777228
6  1.195634 -0.259040 -0.432147  1.196420
7 -2.346814  1.251494  0.261687  0.400886
8  0.845000  0.536683 -2.628224 -0.238449
9  0.246398 -0.548448 -0.295481  0.076117

In [203]: df.iloc[:, (df.iloc[-1] > 0).values]
Out[203]: 
          0         3
0 -1.380064 -1.970113
1 -0.612594 -0.848067
2  1.178626  0.736255
3 -0.909491 -0.605075
4 -1.214366  0.192488
5 -0.283786 -0.777228
6  1.195634  1.196420
7 -2.346814  0.400886
8  0.845000 -0.238449
9  0.246398  0.076117

基本上,此解決方案使用非常基本的Pandas索引 ,尤其是iloc()方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM