Chapter 12 pandas DataFrames
run pip3 install pandas or
run !pip install pandas on rstudio terminal or mac terminal or jupyter notebook
12.1 Data Frames
12.1.1 Data frame creation
Sometimes you may need to create a data frame
## Empty DataFrame
## Columns: []
## Index: []
## Empty DataFrame
## Columns: [A]
## Index: []
Is df2 empty?
There is column name but it is still empty.
## True
How to create a sample data frame?
From a dictionary
my_dict = {'Col_1': [1,2,3,4],
'Col_2': ['a', 'b', 'c', 'd'],
'Col_3': 1984}
my_df = pd.DataFrame(my_dict)
my_df[:5]## Col_1 Col_2 Col_3
## 0 1 a 1984
## 1 2 b 1984
## 2 3 c 1984
## 3 4 d 1984
a_list = [1,2,3,4]
b_list = ['a', 'b', 'c', 'd']
##
df3 = pd.DataFrame({'var1': a_list,
'var2': b_list})
df3## var1 var2
## 0 1 a
## 1 2 b
## 2 3 c
## 3 4 d
12.1.2 Read csv files
## id name city state region
## 0 102669 Alaska Pacific University Anchorage AK West
## 1 101648 Marion Military Institute Marion AL South
## 2 100830 Auburn University at Montgomery Montgomery AL South
## 3 101879 University of North Alabama Florence AL South
## 4 100858 Auburn University Auburn AL South
12.1.3 Data attributes
12.1.3.2 Columns
How to get the column names?
–> Column names are stored in columns attribute.
## Index(['id', 'name', 'city', 'state', 'region'], dtype='object')
## ['id', 'name', 'city', 'state', 'region']
## ['id', 'name', 'city', 'state', 'region']
## id
## name
## city
## state
## region
12.1.3.3 data types in dataframe
## id int64
## name object
## city object
## state object
## region object
## dtype: object
## [dtype('int64'), dtype('O'), dtype('O'), dtype('O'), dtype('O')]
12.1.3.4 index of the data frame
## id name city state region
## 0 102669 Alaska Pacific University Anchorage AK West
## 1 101648 Marion Military Institute Marion AL South
## 2 100830 Auburn University at Montgomery Montgomery AL South
## 3 101879 University of North Alabama Florence AL South
## 4 100858 Auburn University Auburn AL South
## RangeIndex(start=0, stop=100, step=1)
## 0
## 1
## 2
## 3
## 4
12.1.3.5 assign a new index
## id name city state region
## state
## AK 102669 Alaska Pacific University Anchorage AK West
## AL 101648 Marion Military Institute Marion AL South
## AL 100830 Auburn University at Montgomery Montgomery AL South
## AL 101879 University of North Alabama Florence AL South
## AL 100858 Auburn University Auburn AL South
12.1.3.6 index to column
## id name ... region index_column
## 0 102669 Alaska Pacific University ... West 0
## 1 101648 Marion Military Institute ... South 1
## 2 100830 Auburn University at Montgomery ... South 2
## 3 101879 University of North Alabama ... South 3
## 4 100858 Auburn University ... South 4
##
## [5 rows x 6 columns]
- calling
keys()function: output is similar to columns attributes
## Index(['id', 'name', 'city', 'state', 'region'], dtype='object')
column.valuesmethod returns an array of index.
## ['id', 'name', 'city', 'state', 'region']
Using tolist() method with values with given the list of columns.
## ['id', 'name', 'city', 'state', 'region']
Using sorted() method : sorted() method will return the list of columns sorted in alphabetical order.
## ['var1', 'var2']