Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using @Max Fellows' handy example data, we can have a look at it in <code>pandas</code>. [BTW, you should always try to provide a short, self-contained, correct example (see <a href="http://sscce.org/" rel="nofollow">here</a> for more details), so that the people trying to help you don't have to spend time coming up with one.]</p> <p>First, <code>import pandas as pd</code>. Then:</p> <pre><code>In [23]: df = pd.read_csv("sample.csv", names="date time price mag signal".split()) In [24]: df.set_index(["date", "time"], inplace=True) </code></pre> <p>which gives me</p> <pre><code>In [25]: df Out[25]: price mag signal date time 12/28/2012 1:30 10 foo S 2:15 11 bar N 3:00 12 baz S 4:45 13 fibble N 5:30 14 whatsit S 6:15 15 bobs N 7:00 16 widgets S 7:45 17 weevils N 8:30 18 badger S 9:15 19 moose S 11/29/2012 1:30 10 foo N 2:15 11 bar N 3:00 12 baz S 4:45 13 fibble N 5:30 14 whatsit N 6:15 15 bobs N 7:00 16 widgets S 7:45 17 weevils N 8:30 18 badger N 9:15 19 moose N [etc.] </code></pre> <p>We can see which rows have a signal of <code>S</code> easily:</p> <pre><code>In [26]: df["signal"] == "S" Out[26]: date time 12/28/2012 1:30 True 2:15 False 3:00 True 4:45 False 5:30 True 6:15 False [etc..] </code></pre> <p>and we can select using this too:</p> <pre><code>In [27]: df["price"][df["signal"] == "S"] Out[27]: date time 12/28/2012 1:30 10 3:00 12 5:30 14 7:00 16 8:30 18 9:15 19 11/29/2012 3:00 12 7:00 16 12/29/2012 3:00 12 7:00 16 8/9/2008 3:00 12 7:00 16 Name: price </code></pre> <p>This is a <code>DataFrame</code> with every date, time, and price where there's an <code>S</code>. And if you simply want a list:</p> <pre><code>In [28]: list(df["price"][df["signal"] == "S"]) Out[28]: [10.0, 12.0, 14.0, 16.0, 18.0, 19.0, 12.0, 16.0, 12.0, 16.0, 12.0, 16.0] </code></pre> <p><strong>Update</strong>:</p> <p><code>v=[df["signal"]=="S"]</code> makes <code>v</code> a Python <code>list</code> containing a <code>Series</code>. That's not what you want. <code>df["price"][[v and (u and t)]]</code> doesn't make much sense to me either --: <code>v</code> and <code>u</code> are mutually exclusive, so if you and them together, you'll get nothing. For these logical vector ops you can use <code>&amp;</code> and <code>|</code> instead of <code>and</code> and <code>or</code>. Using the reference data again:</p> <pre><code>In [85]: import pandas as pd In [86]: df = pd.read_csv("sample.csv", names="date time price mag signal".split()) In [87]: v=df["signal"]=="S" In [88]: t=df["time"]=="4:45" In [89]: u=df["signal"]!="S" In [90]: df[t] Out[90]: date time price mag signal 3 12/28/2012 4:45 13 fibble N 13 11/29/2012 4:45 13 fibble N 23 12/29/2012 4:45 13 fibble N 33 8/9/2008 4:45 13 fibble N In [91]: df["price"][t] Out[91]: 3 13 13 13 23 13 33 13 Name: price In [92]: df["price"][v | (u &amp; t)] Out[92]: 0 10 2 12 3 13 4 14 6 16 8 18 9 19 12 12 13 13 16 16 22 12 23 13 26 16 32 12 33 13 36 16 Name: price </code></pre> <p>[Note: this question has now become too long and meandering. I suggest spending some time working through the examples in the <code>pandas</code> documentation at the console to get a feel for it.]</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload