简体   繁体   中英

Counting from a column in a Pandas Dataframe

I am attempting to count the number of instances of an element in a column of a Pandas Dataframe based on a set of criteria. I am running into difficulty in a few places.

Here is what I have up to this point. It effectively reads the CSV, drops the duplicates, and sorts df2. I am performing all of these steps in order to isolate the criteria I want to use in the future. Frankly, this may even be an extra step I do not need.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# importing all required modules numpy, pyplot, and pandas

df= pd.read_csv('file.csv')
# reading the CSV file as a pandas dataframe

df2 = df.drop_duplicates(subset="MRCEmp")
df2 = df2.sort_values(["CLNum"])
# creating duplicate dataframe eliminating duplicate pairs
# sorting df2 in ascending order by column "CLNum"

clmax = df2["CLNum"].max()
clmin = df2["CLNum"].min()
# creating variables as int to define the maximum and minimum of the "CLNum: column

for n in df2["CLNum"]:  

    if n not in df2["CLNum"]:

        n = n + 1

    elif n in df2["CLNum"]:
    
        print(df2.loc[df2["CLNum"] == n])
    
        n = n + 1

I should note that not all integers are represented in df2["CLnum"] that is why I inserted the first for loop.

When running this script however, not all of the rows are displayed. clmax = 728 and clmin = 1 , but the final row displayed holds an n value of 283. I cannot find why not all rows are displayed.

尝试熊猫value_counts函数

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.

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