Introduction

This is an introduction.

  • Python Intro
  • Variables, Functions
  • DataFrame

amitkaps.com

Variables and Assignment

In [5]:
x = 11
In [6]:
x
Out[6]:
11
In [9]:
y = 5.5
In [10]:
x + y
Out[10]:
16.5
In [11]:
z = "string"

Lists

In [17]:
items = [1, 2, 5, 6]
In [18]:
items
Out[18]:
[1, 2, 5, 6]
In [20]:
# indexing
items[0]
Out[20]:
1
In [21]:
for item in items:
    print(item)
1
2
5
6

Functions

In [22]:
def square(x):
    return x*x
In [23]:
square(2)
Out[23]:
4
In [25]:
new = []
for item in items:
    new.append(square(item))
In [26]:
new
Out[26]:
[1, 4, 25, 36]

DataFrame

In [28]:
import pandas as pd
In [29]:
data = {"area": ["north", "east", "west", "south", "central"],
       "sales": [5, 25, 15, 20, 10],
       "profit": [2, 8, 6, 5, 3]}
In [33]:
df = pd.DataFrame(data)
In [34]:
df
Out[34]:
area profit sales
0 north 2 5
1 east 8 25
2 west 6 15
3 south 5 20
4 central 3 10
In [36]:
type(data), type(df)
Out[36]:
(dict, pandas.core.frame.DataFrame)
In [39]:
# help
# ?pd.DataFrame
In [41]:
# Headers => columns
df.columns
Out[41]:
Index(['area', 'profit', 'sales'], dtype='object')
In [42]:
# Row => Index
df.index
Out[42]:
RangeIndex(start=0, stop=5, step=1)

Accessing my dataFrame

In [44]:
df["area"]
Out[44]:
0      north
1       east
2       west
3      south
4    central
Name: area, dtype: object
In [46]:
type(df.area)
Out[46]:
pandas.core.series.Series
In [48]:
df.head(2)
Out[48]:
area profit sales
0 north 2 5
1 east 8 25
In [49]:
df.tail(2)
Out[49]:
area profit sales
3 south 5 20
4 central 3 10
In [51]:
# Data back as a list
df.values
Out[51]:
array([['north', 2, 5],
       ['east', 8, 25],
       ['west', 6, 15],
       ['south', 5, 20],
       ['central', 3, 10]], dtype=object)
In [52]:
type(df.values)
Out[52]:
numpy.ndarray

First Way

In [55]:
df
Out[55]:
area profit sales
0 north 2 5
1 east 8 25
2 west 6 15
3 south 5 20
4 central 3 10
In [56]:
df.iloc[0,0]
Out[56]:
'north'
In [61]:
df.loc[0, "area"]
Out[61]:
'north'
In [65]:
df.area[0]
Out[65]:
'north'

Access this part of the dataframe

  • west | 6
  • south | 5
In [66]:
df
Out[66]:
area profit sales
0 north 2 5
1 east 8 25
2 west 6 15
3 south 5 20
4 central 3 10
In [67]:
df.loc[2:3, ["area", "profit"]]
Out[67]:
area profit
2 west 6
3 south 5
In [71]:
df.iloc[2:3, 0:1]
Out[71]:
area
2 west
In [79]:
df.index = [5, 5, 8 , 3, 3]
In [80]:
df
Out[80]:
area profit sales
5 north 2 5
5 east 8 25
8 west 6 15
3 south 5 20
3 central 3 10
In [82]:
df.loc[5:3, ["area", "profit"]]
Out[82]:
area profit
5 north 2
5 east 8
8 west 6
3 south 5
3 central 3
In [83]:
df
Out[83]:
area profit sales
5 north 2 5
5 east 8 25
8 west 6 15
3 south 5 20
3 central 3 10
In [89]:
df1 = df.copy()
In [90]:
df1.iloc[0,0] ="hello0000"
In [91]:
df1
Out[91]:
area profit sales
5 hello0000 2 5
5 east 8 25
8 west 6 15
3 south 5 20
3 central 3 10
In [92]:
df
Out[92]:
area profit sales
5 hello 2 5
5 east 8 25
8 west 6 15
3 south 5 20
3 central 3 10