Import matplotlib, numpy etc


In [ ]:
import numpy as np
from matplotlib import pyplot as plt
plt.style.use('ggplot')
%matplotlib inline

Import pandas


In [ ]:
import pandas as pd

Create numpy array with your data


In [ ]:
A = np.array([[1,2,3,4,5], [10,20,30,40,50], [11,22,33,44,55]])
A

and a list with your index (row names) and column names


In [ ]:
index = ['a','b','c']
cols = ['A','B','C','D','E']

using pandas you can store all this into a 'DataFrame'


In [ ]:
df = pd.DataFrame(data=A, index=index, columns=cols)
df

Accessing via column by names ([ ] operator)


In [ ]:
df['B']

Access via row name (.loc)


In [ ]:
df.loc['a']

Access via row position (.iloc)


In [ ]:
df.iloc[1]

Mixed name and position (.ix)


In [ ]:
df.ix['a',1]

Acessing single elements


In [ ]:
df.loc['a','B']

In [ ]:
df.iloc[0,1]

Fast access to single values


In [ ]:
df.at['a','C']

In [ ]:
df.iat[0,2]

Slicing


In [ ]:
df.iloc[:,0:3]

Slicing with skipped rows


In [ ]:
df.iloc[::2,:]

or you use boolean indexing!

logical comparison returns a DataFrame with boolean entries


In [ ]:
df[['A','C']]>10

these can be used for indexing


In [ ]:
df_g2 = df[df[['A','B']]>2]
df_g2

you can drop all rows that have only NaN entries


In [ ]:
df_g2.dropna( axis=0, how='all')

or all columns with at least one NaN entry


In [ ]:
df_g2.dropna( axis=1, how='any')

selecting rows which contain certain values


In [ ]:
df[ df["A"].isin([1, 11]) ]

Acessing the "raw" numpy data


In [ ]:
df.values

Sorting


In [ ]:
df.sort_values(by="A", ascending=False)

Appending two data frames


In [ ]:
df2 = pd.DataFrame( np.random.rand(3,5), columns=df.columns )
pd.concat( [df, df2])

Dataframes can also be merged (joined)


In [ ]:
df2 = pd.DataFrame( [[50, 'eins'], [5, 'zwei'], [55, 'drei']], columns=["E2", "no"])
df3 = pd.merge(df, df2, left_on="E", right_on="E2")
df3.drop("E2", axis=1, inplace=True)
df3

Importing data using pandas (repetition from Data I/O session)


In [ ]:
diamonds = pd.read_csv('diamonds.csv',index_col=0)

Show only the first 5 rows of this huge data set


In [ ]:
diamonds.head()

or the last 5


In [ ]:
diamonds.tail()

get some infos about the DataFrame


In [ ]:
diamonds.info()

or some basic statistics


In [ ]:
diamonds.describe()

or even the correlation between the columns


In [ ]:
diamonds.corr()

Compute mean values for each cut


In [ ]:
diamonds.groupby("cut").mean()

Plotting

Pandas DataFrames have include the pyplot.plot function


In [ ]:
x = np.linspace(0, 6, 100)
a = np.array( [x, np.sin(x), x*x] ).transpose()
df = pd.DataFrame( a, columns=["x", "sinx", "sqx"] )
df.plot(x="x", y=["sinx", "sqx"])

Scatterplot


In [ ]:
diamonds.plot(x="carat", y="price", kind='scatter')

Histogramm of diamond prices


In [ ]:
diamonds["price"].plot(kind="hist")

Bar plot


In [ ]:
diamonds.groupby("cut")["price"].sum().plot(kind="barh")

Stacked bar plot


In [ ]:
price_per_color = diamonds.groupby( ["cut", "color"] )["price"].sum().unstack()
price_per_color.plot(kind="barh", stacked=True)
#price_per_color

In [ ]:
price_per_color = diamonds.groupby( ["cut", "color"] )["price"].sum().unstack()
price_per_color

In [ ]: