Python Pandas Concat"WHERE"满足条件 [英] Python Pandas Concat "WHERE" a Condition is met
问题描述
如何从许多Python Pandas数据框中连接"特定的列,而在许多数据框中的每个列中的另一列都满足特定条件(在这里俗称条件"X").
在SQL中,使用带有WHERE df2.Col2 ="X"和df3.Col2 ="X"和df4.col2 ="X" ...等(可以动态运行)的JOIN子句将很简单./p>
在我的情况下,我想创建一个大数据框,其中包含来自多个数据框中每个数据框的所有"Col1",但仅包含Col1行值,而相应的Col2行值大于"0.8".如果不满足此条件,则Col1值应为"NaN".
任何想法都将最有帮助!预先感谢!
考虑 pd.DataFrame
s
dfs
> 将pandas导入为pd将numpy导入为npnp.random.seed([3,1415])dfs = [pd.DataFrame(np.random.rand(10,2),column = ['Col1','Col2'])表示_ in range(5)]
我将使用 pd.concat
加入
原始连接
堆积值,而不管它来自何处
pd.concat([dfs中d的[d.Col1.loc [d.Col2.gt(.8)] for d],ignore_index = True)0 0.8504451 0.9348292 0.8798913 0.0858234 0.7396355 0.7005666 0.5423297 0.8820298 0.4962509 0.58530910 0.883372名称:Col1,dtype:float64
加入源信息
使用 keys
参数
pd.concat([dfs中d的[d.Col1.loc [d.Col2.gt(.8)]],keys = range(len(dfs)))0 3 0.8504455 0.9348296 0.8798911 1 0.0858232 0.7396357 0.7005662 4 0.5423293 3 0.8820294 0.4962508 0.5853094 0 0.883372名称:Col1,dtype:float64
另一种方法
使用 query
pd.concat([d.query('Col2> .8').dol中d的col1],keys = range(len(dfs)))0 3 0.8504455 0.9348296 0.8798911 1 0.0858232 0.7396357 0.7005662 4 0.5423293 3 0.8820294 0.4962508 0.5853094 0 0.883372名称:Col1,dtype:float64
How can I "concat" a specific column from many Python Pandas dataframes, WHERE another column in each of the many dataframes meets a certain condition (colloquially termed condition "X" here).
In SQL this would be simple using JOIN clause with WHERE df2.Col2 = "X" and df3.Col2 = "X" and df4.col2 = "X"... etc (which can be run dynamically).
In my case, I want to create a big dataframe with all the "Col1"s from each of the many dataframes, but only include the Col1 row values WHERE the corresponding Col2 row value is greater than "0.8". When this condition isn't met, the Col1 value should be "NaN".
Any ideas would be most helpful! Thanks in advance!
consider the list
dfs
of pd.DataFrame
s
import pandas as pd
import numpy as np
np.random.seed([3,1415])
dfs = [pd.DataFrame(np.random.rand(10, 2),
columns=['Col1', 'Col2']) for _ in range(5)]
I'll use pd.concat
to join
raw concat
stack values without regard to where it came from
pd.concat([d.Col1.loc[d.Col2.gt(.8)] for d in dfs], ignore_index=True)
0 0.850445
1 0.934829
2 0.879891
3 0.085823
4 0.739635
5 0.700566
6 0.542329
7 0.882029
8 0.496250
9 0.585309
10 0.883372
Name: Col1, dtype: float64
join with source information
use the keys
parameter
pd.concat([d.Col1.loc[d.Col2.gt(.8)] for d in dfs], keys=range(len(dfs)))
0 3 0.850445
5 0.934829
6 0.879891
1 1 0.085823
2 0.739635
7 0.700566
2 4 0.542329
3 3 0.882029
4 0.496250
8 0.585309
4 0 0.883372
Name: Col1, dtype: float64
another approach
use query
pd.concat([d.query('Col2 > .8').Col1 for d in dfs], keys=range(len(dfs)))
0 3 0.850445
5 0.934829
6 0.879891
1 1 0.085823
2 0.739635
7 0.700566
2 4 0.542329
3 3 0.882029
4 0.496250
8 0.585309
4 0 0.883372
Name: Col1, dtype: float64
这篇关于Python Pandas Concat"WHERE"满足条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!