DataFrames in pandas are similar to data.frame in R and can hold tabular data.
Every column is a specific type, such as integer or float. This is similar to R in which each column is a class such as integer or numeric.
The means function is a method of the DataFrame and the period is important here. In R, the periods are mostly treated as parts of name.
A dictionary d is created, of the 3 samples, and next a DataFrame is created based on the dictionary.
# ex7.py
from __future__ import division, print_function
from pandas import DataFrame
from numpy.random import choice
x1 = choice(10, size = 10, replace = False)
x2 = choice(10, size = 10, replace = False)
x3 = choice(10, size = 10, replace = False)
d = {'x1':x1,'x2':x2,'x3':x3}
df = DataFrame(d)
print('\nDataFrame df =\n%s' % df)
print('\nFirst Column =\n%s' % df['x1'])
print('\nFirst Row =\n%s' % df.ix[0])
print('\nFirst Column, First Row = %s' % df.loc[0,'x1'])
print('\nAdding ave column')
df['ave'] = df.mean(axis=1)
print('\ndf =\n%s' % df)
print('\nAdding ave row')
df.ix['ave_row'] = df.mean(axis=0)
print('\ndf =\n%s' % df)
#DataFrame df =
# x1 x2 x3
#0 6 9 8
#1 3 1 6
#2 9 5 5
#3 0 3 3
#4 5 2 9
#5 2 8 7
#6 7 7 4
#7 4 0 1
#8 1 6 0
#9 8 4 2
#
#First Column =
#0 6
#1 3
#2 9
#3 0
#4 5
#5 2
#6 7
#7 4
#8 1
#9 8
#Name: x1, dtype: int32
#
#First Row =
#x1 6
#x2 9
#x3 8
#Name: 0, dtype: int32
#
#First Column, First Row = 6
#
#Adding ave column
#
#df =
# x1 x2 x3 ave
#0 6 9 8 7.666667
#1 3 1 6 3.333333
#2 9 5 5 6.333333
#3 0 3 3 2.000000
#4 5 2 9 5.333333
#5 2 8 7 5.666667
#6 7 7 4 6.000000
#7 4 0 1 1.666667
#8 1 6 0 2.333333
#9 8 4 2 4.666667
#
#Adding ave row
#
#df =
# x1 x2 x3 ave
#0 6.0 9.0 8.0 7.666667
#1 3.0 1.0 6.0 3.333333
#2 9.0 5.0 5.0 6.333333
#3 0.0 3.0 3.0 2.000000
#4 5.0 2.0 9.0 5.333333
#5 2.0 8.0 7.0 5.666667
#6 7.0 7.0 4.0 6.000000
#7 4.0 0.0 1.0 1.666667
#8 1.0 6.0 0.0 2.333333
#9 8.0 4.0 2.0 4.666667
#ave_row 4.5 4.5 4.5 4.500000
No comments:
Post a Comment