Week 10: Panda Dataframe Error and doubts

Dear PadhAI,

Requesting you to please resolve the below:

1)meaning of the below ?

def create_df(nRows,nCols,maxRand=10):

  arr=np.random.randint(0,maxRand,(nRows,nCols))

  df=pd.DataFrame(arr)

  df.index=['R'+str(x) for x in np.arange(1,nRows+1)]

  df.columns=['C'+str(x) for x in np.arange(1,nCols+1)]

  return df

2)input: df['mass_'] is df.Mass

output :AttributeError Traceback (most recent call last)
in ()
----> 1 df[‘mass_’] is df.Mass

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in getattr(self, name)
5272 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5273 return self[name]
-> 5274 return object.getattribute(self, name)
5275
5276 def setattr(self, name: str, value) -> None:

AttributeError: ‘DataFrame’ object has no attribute ‘Mass’

also, what is the meaning of the code

  1. df[‘pop’] is df.pop----------meaning of the code ??

(3) df[‘pop’] is df.pop
The code above is checking if the output of df['pop'] and df.pop is same (comparison for equality).

df is a dataframe (rows and columns of data) and it has a column name 'pop'.
Column data in a dataframe can be accessed either as df['pop'] or df.pop, but…

Both are ok, but since ‘pop’ is reserved for a built-in function as well, df.pop will refer the built in function and will give different output than df['pop'] .

(2)

Here df is a dataframe which has a column name ‘mass’, but you are accessing it as ‘Mass’ (capital M). Since there is no column name ‘Mass’, there is an error.

(1)

  • The code has function create_df first creat a matrix arr or given number of rows and columns.
  • Next a dataframe df is initialised with matrix arr (dataframe is also rows and columsn of data only) but it has an explicit index and names for columns. (Regular matrix don’t have column names or explicit index)
  • Last 2 lines basically initialise index numbers as R1, R2… and column names as C1, C2 etc.