简体   繁体   English

计算数据框中列中列中的每个值

[英]Count each value in a column within the column in dataframe

I am struggling to do the below and was wondering if you can help me.我正在努力执行以下操作,想知道您是否可以帮助我。

I have a pandas data frame of 2 columns.我有一个 2 列的熊猫数据框。 I want to count each value in column A within Column A. If the occurrence is more than 1, print('hello')我想计算 A 列中 A 列中的每个值。如果出现次数超过 1,则 print('hello')

For instance,例如,

for i in range(1, len(DF)) **if count(DF.iloc[[i],[1]].values)>1:** print('hello') any help please? for i in range(1, len(DF)) **if count(DF.iloc[[i],[1]].values)>1:** print('hello')什么帮助吗?

Thanks, H谢谢,H

If you want to print 'hello' if there is more than one occurence:如果要在出现多次时打印 'hello':

if (df['A'] > 1).count() > 1:
    print('hello')

If you want to print 'hello' at each occurence:如果你想在每次出现时打印 'hello':

You could use df.apply() to apply a printing lambda function to your column:您可以使用df.apply()将打印 lambda 函数应用于您的列:

df['A'].apply(lambda x: print('hello') if x > 1 else None)

Or count all values in A which are greater than one and print in a loop:或者计算 A 中所有大于 1 的值并循环打印:

for i in range(0, (df['A'] > 1).count()):
    print('hello')

I think you need Series.value_counts我认为你需要Series.value_counts

if you want to print once hello if there is any value that is repeated use:如果你想打印一次 hello 如果有任何重复使用的值:

if df['A'].value_counts().gt(1).any():
    print('hello')

if you want to print hello for each value in the series that is repeated:如果要为重复的系列中的每个值打印 hello:

print('Hello\n'*df['A'].map(df['A'].value_counts().gt(1)).sum())

if you want to print hello for each unique value in the series that is repeated如果要为重复的系列中的每个唯一值打印 hello

print('Hello\n'*df['A'].value_counts().gt(1).sum())

Example例子

df = pd.DataFrame({'A':[1,2,3,1,4,4,5,7,8,9,3]})
print(df)

    A
0   1
1   2
2   3
3   1
4   4
5   4
6   5
7   7
8   8
9   9
10  3

if df['A'].value_counts().gt(1).any():
    print('hello\n')
print('2 Method\n')    
print('Hello\n'*df['A'].map(df['A'].value_counts().gt(1)).sum())
print('3 Method\n')
print('Hello\n'*df['A'].value_counts().gt(1).sum())

Output输出

hello

2 Method

Hello
Hello
Hello
Hello
Hello
Hello

3 Method

Hello
Hello
Hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM