例如,我有这个数据框:
d = {'data_1' : pd.Series([1., 2., 3., 4., 1], index=['a', 'b', 'c', 'd', 'class']),
'data_2' : pd.Series([5., 6., 7., 8., 0], index=['a', 'b', 'c', 'd', 'class']),
'data_3' : pd.Series([9., 10., 11., 12., 1], index=['a', 'b', 'c', 'd', 'class'])}
df = pd.DataFrame(d)
以便输出我得到:
data_1 data_2 data_3
a 1.0 5.0 9.0
b 2.0 6.0 10.0
c 3.0 7.0 11.0
cls 1.0 0.0 1.0
我想要做的是根据“ cls ”行中报告的值创建过滤器,并根据两个可能的类将数据框拆分为两个
data_1 data_3
a 1.0 9.0
b 2.0 10.0
c 3.0 11.0
cls 1.0 1.0
和
data_2
a 5.0
b 6.0
c 7.0
cls 0.0
答案 0 :(得分:2)
将boolean indexing
与 [PXButton]
[PXUIField(DisplayName = "Generate")]
public virtual void GenerateInv()
{
InventoryItemExt ext = Base.Item.Cache.GetExtension<InventoryItemExt>(Base.Item.Current);
Base.Item.Current.InventoryCD = ext.UsrInvId;
Base.Item.Cache.Insert(Base.Item.Current);
}
一起使用 - 首先按条件选择所有列,然后按loc
选择所选行:
index
行df1 = df.loc[:, df.loc['class'] == 1]
df2 = df.loc[:, df.loc['class'] == 0]
的所有唯一值也可能创建dicionary:
class
s = df.loc['class']
dfs = {k:df[v.index] for k, v in s.groupby(s)}
print (dfs)
{0.0: data_2
a 5.0
b 6.0
c 7.0
d 8.0
class 0.0, 1.0: data_1 data_3
a 1.0 9.0
b 2.0 10.0
c 3.0 11.0
d 4.0 12.0
class 1.0 1.0}
答案 1 :(得分:1)
动态地执行
In [889]: s = df.loc['class']
In [890]: dfs = {k: df[s[s==v].index] for k, v in enumerate(s.unique())}
# or dfs = {v: df[s[s==v].index] for v in s.unique()} -- for value based keys
In [891]: dfs[0]
Out[891]:
data_1 data_3
a 1.0 9.0
b 2.0 10.0
c 3.0 11.0
d 4.0 12.0
class 1.0 1.0
In [892]: dfs[1]
Out[892]:
data_2
a 5.0
b 6.0
c 7.0
d 8.0
class 0.0