English 中文(简体)
我们需要一部显示数字价值的法典,而不是NaN。
原标题:We need a code that shows the counted value as a number, not NaN
import pandas as pd

df = pd.DataFrame({ A : [ ker ,  2 ,  3 ,  4 ],  B : [ 4 ,  5 ,  6 ,  ker ],  C : [ 4 ,  13 ,  2.5ser , ker ],  D : [ 4 ,  5 ,  6 , 2.5ser ]})
df[ count ] = df.iloc[0, :].str.count( ker ).fillna(0)

print(df)

The count column value is nan . I want a code that counts the value of ker .

问题回答

预期的逻辑不明确,但我想大家可能希望:

df[ count ] = df.apply(lambda s: s.str.count( ker )).sum(axis=1)

请注意,count将计算每一条列中的 ker ( kerxker 。 如果您期望或只想计算一个<代码> ker ,则每一方可使用:

df[ count ] = df.apply(lambda s: s.str.contains( ker )).sum(axis=1)

如果您希望exactcompes(而不是子孙),则简单使用:

df[ count ] = df.eq( ker ).sum(axis=1)

产出:

     A    B       C       D  count
0  ker    4       4       4      1
1    2    5      13       5      0
2    3    6  2.5ser       6      0
3    4  ker     ker  2.5ser      2

下面是显示差异的另一个例子:

df = pd.DataFrame({ A : [ ker ,  kernel ,  3 ,  4 ],
                    B : [ 4 ,  5 ,  6 ,  kerker ],
                    C : [ 4 ,  13 ,  2.5ser , ker ],
                    D : [ 4 ,  5 ,  6 , 2.5ser ]})

cols = [ A ,  B ,  C ,  D ]
df[ count1 ] = df[cols].apply(lambda s: s.str.count( ker )).sum(axis=1)
df[ count2 ] = df[cols].apply(lambda s: s.str.contains( ker )).sum(axis=1)
df[ count3 ] = df[cols].eq( ker ).sum(axis=1)

产出:

        A       B       C       D  count1  count2  count3
0     ker       4       4       4       1       1       1
1  kernel       5      13       5       1       1       0
2       3       6  2.5ser       6       0       0       0
3       4  kerker     ker  2.5ser       3       2       1
df[ count ] = df.isin(("ker",)).sum(1)

.isin() 方法回收了具有真实或合法价值的数据框架,.sum(<>>>/code>方法,然后退回了带有若干发生率的一系列数据,因为真实价值被作为1处理,而法尔则被看作0。


结果:

     A    B       C       D  count
0  ker    4       4       4      1
1    2    5      13       5      0
2    3    6  2.5ser       6      0
3    4  ker     ker  2.5ser      2




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...