滤波行只有一个列中的一个具有值和创建列以在熊猫中附加记分卡

发布于 2025-02-11 20:16:44 字数 673 浏览 0 评论 0原文

我的目标是过滤行,其中只有一列中的一个没有NAN的值。一旦我过滤了这些行,我想为每列发生多少个实例创建一个记分卡。

示例输入:

index   id      pass shoot flick through-ball
22450   0123    NaN  NaN   NaN   600
22451   6565    NaN  NaN   NaN   625
22452   1212    123  NaN   454   NaN
22453   0101    NaN  NaN   119   NaN
22454   1234    NaN  056   98    NaN    

过滤的预期结果:

index   id      pass shoot flick through-ball
22450   0123    NaN  NaN   NaN   600
22451   6565    NaN  NaN   NaN   625
22453   0101    NaN  NaN   119   NaN

最终预期输出表:

actions        unique_count 
pass           0
shoot          0
flick          1
through-ball   2

My goal is to filter rows where only one of the columns has a value that is not NaN. Once I have those rows filtered, I want to create a scorecard for how many instances this happened per column.

Example input:

index   id      pass shoot flick through-ball
22450   0123    NaN  NaN   NaN   600
22451   6565    NaN  NaN   NaN   625
22452   1212    123  NaN   454   NaN
22453   0101    NaN  NaN   119   NaN
22454   1234    NaN  056   98    NaN    

Expected result from filtering:

index   id      pass shoot flick through-ball
22450   0123    NaN  NaN   NaN   600
22451   6565    NaN  NaN   NaN   625
22453   0101    NaN  NaN   119   NaN

Final expected output table:

actions        unique_count 
pass           0
shoot          0
flick          1
through-ball   2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

护你周全 2025-02-18 20:16:44
df.set_index(['index', 'id'], inplace=True)
out = df[df.count(axis=1).eq(1)]
print(out)

counts = out.count().reset_index(name='unique_count').rename(columns={'index':'actions'})
print(counts)

输出:

            pass  shoot  flick  through-ball
index id
22450 123    NaN    NaN    NaN         600.0
22451 6565   NaN    NaN    NaN         625.0
22453 101    NaN    NaN  119.0           NaN

        actions  unique_count
0          pass             0
1         shoot             0
2         flick             1
3  through-ball             2
df.set_index(['index', 'id'], inplace=True)
out = df[df.count(axis=1).eq(1)]
print(out)

counts = out.count().reset_index(name='unique_count').rename(columns={'index':'actions'})
print(counts)

output:

            pass  shoot  flick  through-ball
index id
22450 123    NaN    NaN    NaN         600.0
22451 6565   NaN    NaN    NaN         625.0
22453 101    NaN    NaN  119.0           NaN

        actions  unique_count
0          pass             0
1         shoot             0
2         flick             1
3  through-ball             2
浪漫人生路 2025-02-18 20:16:44

要过滤数据框,我们可以按列计算非NAN值,

cols = ['pass', 'shoot', 'flick', 'through-ball']

filtered = df[df[cols].notna().sum(axis=1).eq(1)]
print(filtered)

   index    id  pass  shoot  flick  through-ball
0  22450   123   NaN    NaN    NaN         600.0
1  22451  6565   NaN    NaN    NaN         625.0
3  22453   101   NaN    NaN  119.0           NaN

我们可以循环列以获取每个列中的唯一值计数

out = pd.DataFrame([[col, filtered[col].nunique()] for col in cols],
                   columns=['actions', 'unique_count'])
print(out)

        actions  unique_count
0          pass             0
1         shoot             0
2         flick             1
3  through-ball             2

To filter the dataframe, we can count the not nan value along columns

cols = ['pass', 'shoot', 'flick', 'through-ball']

filtered = df[df[cols].notna().sum(axis=1).eq(1)]
print(filtered)

   index    id  pass  shoot  flick  through-ball
0  22450   123   NaN    NaN    NaN         600.0
1  22451  6565   NaN    NaN    NaN         625.0
3  22453   101   NaN    NaN  119.0           NaN

We can loop the columns to get unique value count in each column

out = pd.DataFrame([[col, filtered[col].nunique()] for col in cols],
                   columns=['actions', 'unique_count'])
print(out)

        actions  unique_count
0          pass             0
1         shoot             0
2         flick             1
3  through-ball             2
划一舟意中人 2025-02-18 20:16:44

我们可以对其进行过滤

df = df[df.notna().sum(1)==3]
Out[262]: 
   index    id  pass  shoot  flick  through-ball
0  22450   123   NaN    NaN    NaN         600.0
1  22451  6565   NaN    NaN    NaN         625.0
3  22453   101   NaN    NaN  119.0           NaN

,然后count

df.drop(['index', 'id'],axis=1).count().iloc[2:]
Out[267]: 
pass            0
shoot           0
flick           1
through-ball    2
dtype: int64

We can filter it

df = df[df.notna().sum(1)==3]
Out[262]: 
   index    id  pass  shoot  flick  through-ball
0  22450   123   NaN    NaN    NaN         600.0
1  22451  6565   NaN    NaN    NaN         625.0
3  22453   101   NaN    NaN  119.0           NaN

Then count

df.drop(['index', 'id'],axis=1).count().iloc[2:]
Out[267]: 
pass            0
shoot           0
flick           1
through-ball    2
dtype: int64
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文