Creation
DataFrame can be created using following constructor with flexibility to provide data, index name, column names and column data type as follows: |
pd.DataFrame( data=None, index: 'Axes | None' = None, columns: 'Axes | None' = None, dtype: 'Dtype | None' = None, copy: 'bool | None' = None, )
|
Create DataFrame from list
One of the quickest ways to generate DataFrame in real-life would be to load data directly from SQL database, CSV or excel file. DataFrame can be created in different ways from various data sources. |
import pandas as pd # list of strings list1 = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven'] #list of numbers list2 = [1,2,3,4,5,6,7] # Calling DataFrame constructor on lists df = pd.DataFrame({'Words':list1, 'Number':list2}) print(df)
|
|
|
Shallow copy
#Access shallow/reference copy which generates "SettingWithCopyWarning" warning df1 = df df1['Words'][0] = 'One only'
|
Deep Copy
Data contained in the DataFrame can be modfied by slicing and using various selection methods |
#Access data and manipulate using "deep" copy instead of shallow view change df1 = df[['Words']].copy() df1.iloc[0] = 'One only' df1
|
|
|
Select Column
#Subset: select one of the columns df['Words']
|
Select row with row index
#Select row with row index df.iloc[1]
|
Select row with .loc[]
#Select row with row index df.iloc[1]
|
|