I am struggling to do the below and was wondering if you can help me.
I have a pandas data frame of 2 columns. I want to count each value in column A within Column A. If the occurrence is more than 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?
Thanks, H
If you want to print 'hello' if there is more than one occurence:
if (df['A'] > 1).count() > 1:
print('hello')
If you want to print 'hello' at each occurence:
You could use df.apply()
to apply a printing lambda function to your column:
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:
for i in range(0, (df['A'] > 1).count()):
print('hello')
I think you need Series.value_counts
if you want to print once hello if there is any value that is repeated use:
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:
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.