Pandas - 过滤文本和数据

Pandas - Filtering text and data

我们刚刚学习了如何过滤 Python 中的一些 pandas,所以我想我会在 public 数据集上尝试一下。 (http://data.wa.aemo.com.au/#stem-bids-and-offers)

为此我使用了八月份的数据。

我给自己设定的挑战是仅根据 $/MWh > 0 进行过滤,并且必须在出价时进行过滤。我们已经学习了如何使用 np.logical_and 进行过滤,但我发现的问题是我可以根据数字或逻辑进行过滤。两者都不是。

我有一种方法可以让我获得所需的数据和可视化效果,但我确信有一种更有效的方法可以按文本和数字字段进行过滤。我的方法的问题是它仅在字符大小不同时才有效。即如果它说 Bid 或 Fib。我会选择两者。我想接标。 谁能给我指出正确的方向?

这是我的代码:

#Task: I want to filter out ONLY positive $/MWh bids
#This requires 2 filters - 1 to filter out the $MWh > 0 and 1 to filter by Bids

# Try converting this to a numpy array and using the filtering mechanisms there
import numpy as np
df = pd.read_csv('stem-bids-and-offers-2017-08.csv')
df.head(5)
#I don't know how to filter by 'text' just yet so I will have to use another way which is using the len function
#This will reduce the bid/offer field to characters

df['boLength'] = df['Bid or Offer'].apply(len)
df.head(5)
filtByPriceBid = np.logical_and(df['Price ($/MWh)'] > 0, df['boLength'] == 3)
filtByPriceBid.head(5)

df2 = df[filtByPriceBid]
df2.head(10)

sns.kdeplot(df2['Price ($/MWh)'], shade=True)

PS:我附上了由此产生的 KDE 图。如果有人也想对此提供解释,请随时这样做!我期待一个标准化的分布,但不幸的是,事实并非如此。

希望这就是您要找的。

您可以使用 & 将多个过滤器放在一起

sns.kdeplot(df[(df['Price ($/MWh)'] > 0) & (df['Bid or Offer']=='Bid')]['Price ($/MWh)'], shade=True)