Computer History Notes - Herong's Tutorial Notes - v3.14, by Herong Yang
Python pandas.DataFrame - The Table Class
This section describes pandas.DataFrame/pandas.core.frame.DataFrame class, which represents a table of data with rows and columns.
Pandas is a third party Python package for data manipulation and analysis in Python, providing powerful and flexible data structures.
pandas.DataFrame/pandas.core.frame.DataFrame is a class provided in Pandas to represent a tabular structure with potentially heterogeneously-typed columns.
Main features of pandas.DataFrame are:
Here are some basic properties, operations and methods provided in pandas.DataFrame class.
1. pd.DataFrame() - Method to construct a new DataFrame object. The example below uses a JSON string to provide input.
>>> import pandas as pd
>>>
df = pd.DataFrame({
"Name": [
"Braund, Mr. Owen Harris",
"Allen, Mr. William Henry",
"Bonnell, Miss. Elizabeth",
],
"Age": [22, 35, 58],
"Sex": ["male", "male", "female"],
})
>>> type(df)
<class 'pandas.core.frame.DataFrame'>
>>> print(df)
Name Age Sex
0 Braund, Mr. Owen Harris 22 male
1 Allen, Mr. William Henry 35 male
2 Bonnell, Miss. Elizabeth 58 female
2. df[col_name] or df.col_name - Operation to return a Series object representing data elements of the given column.
>>> age = df["Age"] >>> age = df.Age >>> type(age) <class 'pandas.core.series.Series'> >>> print(age) 0 22 1 35 2 58 Name: Age, dtype: int64
3. df[col_name][index], df.col_name[index] or df.at[index, col_name] - Operation to return the data element of the given index and the given column.
>>> a2 = df["Age"][2] >>> a2 = df.Age[2] >>> a2 = df.at[2, "Age"] >>> type(a2) <class 'numpy.int64'> >>> print(a2) 58
4. df.iat[i, j] - Operation to return the data element of the given row position and the given column position of the DataFrame.
>>> a2 = df.iat[2, 1] >>> type(a2) <class 'numpy.int64'> >>> print(a2) 58
5. df.columns - Property holding an Index object representing the column list.
>>> cols = df.columns >>> type(cols) <class 'pandas.core.indexes.base.Index'> >>> print(cols) Index(['Name', 'Age', 'Sex'], dtype='object')
6. df.shape - Property holding shape (number of rows and number of columns) of of the DataFrame.
>>> print(df.shape) (3, 3)
7. df.append() - Method to append a Dict object as a row to the DataFrame.
>>> df.append({"Name": "John Smith", "Age": 18, "Sex": "male"})
8. df.groupby(column_list) - Method to return pandas.core.groupby.generic.DataFrameGroupBy object, which supports count() and other methods.
>>> grp = df.groupby(['Sex']).count()
Name Age
Sex
female 1 1
male 2 2
Table of Contents
2009 - Bitcoin Cryptocurrency Invented by Satoshi Nakamoto
2002 - .NET Framework Developed by Microsoft
1995 - PHP: Hypertext Preprocessor Created by Rasmus Lerdorf
1995 - Java Language Developed by Sun Microsystems
►1991 - Python Language Designed by Guido van Rossum
Using Python Shell at python.org
Python "while" Statement for Execution Loop
►Python pandas.DataFrame - The Table Class
1991 - WWW (World Wide Web) Developed by Tim Berners-Lee
1991 - Gopher Protocol Created by a University of Minnesota Team
1984 - X Window System Developed a MIT Team
1984 - Macintosh Developed by Apple Inc.
1983 - "Sendmail" Mail Transfer Agent Developed by Eric Allman
1979 - The Tcsh (TENEX C Shell) Developed by Ken Greer
1978 - Bash (Bourne-Again Shell) Developed by Brian Fox
1978 - The C Shell Developed by Bill Joy
1977 - The Bourne Shell Developed by Stephen Bourne
1977 - Apple II Designed by Steve Jobs and Steve Wozniak
1976 - vi Text Editor Developed by Bill Joy
1974 - Internet by Vinton Cerf
1972 - C Language Developed by Dennis Ritchie
1971 - FTP Protocol Created by Abhay Bhushan
1970 - UNIX Operating System Developed by AT&T Bell Labs