- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
pandas.DataFrame.query()
方法非常适合在加载或绘图时(预/后)过滤数据。它对于方法链特别方便。
我发现自己经常想将相同的逻辑应用于 pandas.Series
,例如在完成诸如返回 pandas.Series
的 df.value_counts
之类的方法之后。
假设有一个巨大的表格,其中包含 Player、Game、Points
列,我想绘制得分超过 14 乘以 3 的球员的直方图。我首先必须对每个玩家的分数求和 (groupby -> agg
),这将返回一个由 ~1000 名玩家组成的系列赛及其总分。应用 .query
逻辑,它看起来像这样:
df = pd.DataFrame({
'Points': [random.choice([1,3]) for x in range(100)],
'Player': [random.choice(["A","B","C"]) for x in range(100)]})
(df
.query("Points == 3")
.Player.values_count()
.query("> 14")
.hist())
我找到的唯一解决方案迫使我做一个不必要的分配并打破方法链:
(points_series = df
.query("Points == 3")
.groupby("Player").size()
points_series[points_series > 100].hist()
方法链和查询方法有助于保持代码清晰,同时子集过滤很快就会变得困惑。
# just to make my point :)
series_bestplayers_under_100[series_prefiltered_under_100 > 0].shape
请帮我走出困境!谢谢
最佳答案
如果我理解正确,你可以添加query("Points > 100")
:
df = pd.DataFrame({'Points':[50,20,38,90,0, np.Inf],
'Player':['a','a','a','s','s','s']})
print (df)
Player Points
0 a 50.000000
1 a 20.000000
2 a 38.000000
3 s 90.000000
4 s 0.000000
5 s inf
points_series = df.query("Points < inf").groupby("Player").agg({"Points": "sum"})['Points']
print (points_series)
a = points_series[points_series > 100]
print (a)
Player
a 108.0
Name: Points, dtype: float64
points_series = df.query("Points < inf")
.groupby("Player")
.agg({"Points": "sum"})
.query("Points > 100")
print (points_series)
Points
Player
a 108.0
另一种解决方案是 Selection By Callable :
points_series = df.query("Points < inf")
.groupby("Player")
.agg({"Points": "sum"})['Points']
.loc[lambda x: x > 100]
print (points_series)
Player
a 108.0
Name: Points, dtype: float64
编辑问题的编辑答案:
np.random.seed(1234)
df = pd.DataFrame({
'Points': [np.random.choice([1,3]) for x in range(100)],
'Player': [np.random.choice(["A","B","C"]) for x in range(100)]})
print (df.query("Points == 3").Player.value_counts().loc[lambda x: x > 15])
C 19
B 16
Name: Player, dtype: int64
print (df.query("Points == 3").groupby("Player").size().loc[lambda x: x > 15])
Player
B 16
C 19
dtype: int64
关于python - pandas Series (pandas.Series.query()) 是否有查询方法或类似方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40171498/
有时,我倾向于(重复)执行next next a 来获取特定元素。当您需要 2 次或更少的遍历时,这很有效。然而,它很快就会变得很麻烦。对于这个简单的情况,循环的开销太大。 幸运的是,如果您知道位置,
我在使用值为 numpy 数组的 pandas.Series 时遇到了以下奇怪的行为。 % s = pd.Series([5,2], index=[6,7]) %s.loc[6] 5 <-- ret
我有一个看起来像这样的数据框(小版本): A B C 0 125 ADB [AF:12] 1 189 ACB [AF:78, AF:85, AF:98] 2 148 ADB
我在 Pandas (s1) 中创建了一个系列,用于根据原始 DataFrame 中的列 ('d1') 计算这些固定数字 (1-14) 的实例数。我想要的显示在这里(时报); s1 Last
pandas series 有两个密切相关的属性:Series.index 和 Series.index.values。 这两个中的第一个返回某些 pandas 索引类型的当前索引。它是可变的,可用于
我正在尝试使用 KNNClassifier 训练模型。我将数据拆分如下: X_train, X_test, y_train, y_test = train_test_split(X_bow, y, t
我只是尝试对我的数据框进行排序并使用了以下函数: df[df.count >= df.count.quantile(.95)] 返回错误: AttributeError: 'function' obj
我试过了 print(type(numbers[2])) numbers[2].tolist() print(type(numbers[2])) 那是行不通的。我得到了 Numbers 是一个矩阵
我想从时间戳中减去日期。settings.dataset_end_date 是一个 pandas._libs.tslibs.timestamps.Timestamp引用['date_of_patent
我有一个带有数据的 pandas.core.series.Series 0 [00115840, 00110005, 001000033, 00116000... 1 [00267285,
s = pd.Series( nr.randint( 0, 10, 5 ), index=nr.randint(0, 10, 5 ) ) s 输出 1 3 7 6 2 0 9
pandas.DataFrame.query() 方法非常适合在加载或绘图时(预/后)过滤数据。它对于方法链特别方便。 我发现自己经常想将相同的逻辑应用于 pandas.Series,例如在完成诸如返
这个问题在这里已经有了答案: Difference between map, applymap and apply methods in Pandas (11 个回答) 去年关闭。 Series.ma
我正在总结一系列中的值,但根据我如何做,我会得到不同的结果。我试过的两种方法是: sum(df['series']) df['series'].sum() 为什么它们会返回不同的值? 示例代码。 s
我有一个字符串说 type(abc) >>str 我想把它转换成 pandas.core.series.Series。 我在 pandas 文档中看到有一段代码 pd.to_string() 将 pa
我有一个字符串说 type(abc) >>str 我想把它转换成 pandas.core.series.Series。 我在 pandas 文档中看到有一段代码 pd.to_string() 将 pa
这个问题在这里已经有了答案: Pandas: select DF rows based on another DF (5 个答案) 关闭 5 年前。 如果我有一个包含开始时间和结束时间的 DataF
我尝试了 Series.index 和 Series.keys() 并且输出是相似的。我找不到它们之间的显着差异。它们是否适用于某些特殊条件? 我在 Anaconda 上的 Jupyter Noteb
我有一个(非常大的)系列,其中包含关键字(例如,每行包含多个由“-”分隔的关键字 In[5]: word_series Out[5]: 0 the-cat-is-pink 1
我需要使用 pandas.read_excel 通过 Python 获取 Excel 电子表格最后一个单元格的值。该单元格包含一个日期,我需要将其分配给 Python 脚本中的变量。格式为2018-1
我是一名优秀的程序员,十分优秀!