I have prepared the following code:
data = []
for i in range(4, 8):
for value in round(df["month"] / 12, 1) #converting months to years
if value > i:
data.append(value)
What I am wanting to do is count the number of values stored in this list data
which are greater than i
and then store this information in a separate list so I can create a data frame.
I would get something like this:
Description | Count |
---|---|
> 4 years | 56 |
> 5 years | 456 |
I have tried with a lambda
function but this does not seem to work:
data = []
for i in range(4, 8):
for value in round(df["month"] / 12, 1)
if value > i:
data.append(value)
counts = sum(map(lambda x: if x > i, data)))
What can I add to my existing code to get what I want?
CodePudding user response:
You can just use this line of code. It's not clear if this is the output you want, since we can't look at the data.
dataFrame = pd.DataFrame(round(df["month"] / 12, 1).value_counts(dropna=True))
Or you can alternatively use this to get the table you mention as a dataframe
df['foo'] = round(df["month"] / 12, 1)
dataFrame = pd.DataFrame(columns=['Description','Count'])
for i in range(4,8):
dataFrame.loc[4-i] = [">" str(i) " years", (df[df['foo'] > i].foo.count())]
CodePudding user response:
Or just
over4=(round(df.month/12)>=4).sum()
Note: one good rule of thumb to keep in mind "If I am iterating over pandas rows, I am very probably doing something wrong"
To create your dataframe
overs=pd.DataFrame([[f"≥{i}",(round(df.month/12)>=i).sum()] for i in range(4,8)], columns=["Description", "count"])