Data Frame Notes1
Data Frame Notes1
known as Indexes
1 Data data takes various forms like ndarray, series, map, lists, dict, constants and
also another DataFrame.
2 Index For the row labels, the Index to be used for the resulting frame is Optional
Default np.arrange(n) if no index is passed.
3 Columns For column labels, the optional default syntax is - np.arrange(n). This is
only true if no index is passed.
5 Copy This command (or whatever it is) is used for copying of data, if the default is
False.
Lists
dictionary
Series
Numpy ndarrays
Another DataFrame
Empty DataFrame
Columns: []
Index: []
0
0 10
1 20
2 30
3 40
Name Occupation
0 Shraddha Doctor
1 Shanti Teacher
2 Monica Engineer
Example 1:
>>> Mydict= [{'Won': 15, 'Loose': 2},{'Won': 5, 'Loose': 10},
{'Won': 8, 'Loose': 9},{'Won':4}]
>>> df = pd.DataFrame(Mydict)
>>> df
Loose Won
0 2.0 15
1 10.0 5
2 9.0 8
3 NaN 4
Example 2:
>>> Mydict=[{'Won': 15, 'Loose': 2},{'Won': 5, 'Loose': 10},{'Won': 8, 'Loose':
9}]
>>> df = pd.DataFrame(Mydict, index=['India', 'Pakistan','Autralia'])
>>> df
Loose Won
India 2 15
Pakistan 10 5
Autralia 9 8
>>> df1
>>> df2
Chemistry Maths
Student1 78.0 78
Student2 NaN 67
Student3 NaN 87
>>> df3
>>> df3['Physics']=[45,56,65]
>>> df3
English Chemistry Maths Physics
Student1 78 78 78 45
Student2 98 70 67 56
Student3 89 90 87 65
We can add new column using Data ,stored in existing Frame
>>> df3['Total']=df3.English+df3.Chemistry+df3.Maths+df3.Physics
Look a new Column
>>> df3 Total has been added
English Chemistry Maths Physics Total with total of marks in
Student1 78 78 78 45 279
Student2 98 70 67 56 291
other subjects
Student3 89 90 87 65 331
>>> df3
English Chemistry Maths
Student1 78 78 78
Student2 98 70 67
Student3 89 90 87
Student4 45 67 45
ii. To add/Modify row with by specifying row index no.
>>> df3.iloc[3]=[45,67,45]
>>> df3
English Chemistry Maths
Student1 78 78 78
Student2 98 70 67
Student3 89 90 87
Student4 45 67 45
>>> df3.iloc[3]=[65,77,90]
>>> df3
English Chemistry Maths
Student1 78 78 78
Student2 98 70 67
Student3 89 90 87
Student4 65 77 90
>>> df3
English Chemistry Maths Physics
Student1 78 78 78 45
Student2 98 70 67 56
Student3 89 90 87 65
>>> df3.English
Student1 NaN
Student2 NaN
Student3 NaN
Name: English, dtype: float64
>>> df3.loc['Student3']
English 89.0
Chemistry 90.0
Maths 87.0
Physics 66.0
66.0