DF 1
DF 1
• SERIES • DATAFRAME
• Series is 1-Dimensional • DF is 2-dimensional
• It is used to store single • It is used to store
column of data. multiple columns of
• Elements in Series must data.
be homogenous. • Elements my be
• Elements are accessed heterogeneous.
using a single index. • Elements are accessed
• Size of series is using a two index.
immutable. • Size of DF is mutable.
pandas.DataFrame()
• A pandas DataFrame can be created using the
following constructor −
• pandas.DataFrame( data, index, columns,
dtype, copy)
• data
• Data can be ndarray, series, lists, dict,
constants and also another DataFrame.
• index
• For the row labels, the Index to be used for
the resulting frame is Optional Default
np.arange(n) if no index is passed.
• columns
• For column labels, the optional default syntax
is - np.arange(n). This is only true if no index is
passed.
• dtype
• Data type of each column.
• copy
• This command (or whatever it is) is used for
copying of data, if the default is False.
Create DataFrame
import pandas as pd
data = {'Name':['T', ‘J', ‘Se', ‘R'],'Age':[28,34,29,42]}
df = pd.DataFrame(data)
print(df)
Creating DataFrame using Series
import pandas as pd
stud_marks = pd.Series({‘a’:80,’b’:82,’M’:67,’R’:90,’P’:99})
stud_age=pd.Series({‘a’:32,’b’:22,’M’:30,’R’:31,’P’:42})
stud_df = pd.DataFrame({‘Marks’:stud_marks,’Age’:stud_age})
print(stud_df)
Creating DataFrame using Dictionary
import pandas as pd
stud =
{‘Name’:[‘P’,’R’,’A’,’J’,’B’],’Eng’:[67,76,75,88,92],
‘IP’:[99,99,98,97,98],’Maths’:[98,99,97,98,90]}
df = pd.DataFrame(stud)
print(df)