Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23222

IslandT: Find the average negative values from the DataFrame

$
0
0
  1. Create a Pandas’ DataFrame object.
  2. Find the average values from only the negative data values.

Hello and welcome back, since a few days ago I am not posting anything on this website because during these few days I am busy working on a new Blender project (below) for mine new game character. Besides that, I also surf Google and watch lots of Youtube videos. But starting from tomorrow I will start to work hard again for the next game project plus write more articles for this site, so stay tuned!

In this chapter we will continue to explore the Pandas’ DataFrame object plus introduce the new Series object with a simple python program.

We will create a new DataFrame object and then iterate through the data within the DataFrame table, finally, we will sum up those negative values then print out their average. Below is the entire program, what do you think about this one, leave your comment below this post.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(6, 4), index=[0, 1, 2, 3, 4, 5], columns=['A', 'B', 'C', 'D'])

print(df)

sum = 0
count = 0

for label, index in df.items():
    arr = index.values # Series object returns the array list for the data under column A B C and D
    for number in arr:
        if(number < 0):
            sum += number
            count+=1

print("Average Negative Values : ")
print(sum/count)
The DataFrame table and the average negative values

There we go, the next article will be a long one and contains lots of information, so stay tuned!


Viewing all articles
Browse latest Browse all 23222