In [2]:
import numpy as np
from matplotlib import pyplot as plt
plt.style.use('ggplot')
%matplotlib inline
In [3]:
import pandas as pd
In [4]:
A = np.array([[1,2,3,4,5], [10,20,30,40,50], [11,22,33,44,55]])
A
Out[4]:
In [5]:
index = ['a','b','c']
cols = ['A','B','C','D','E']
In [6]:
df = pd.DataFrame(data=A, index=index, columns=cols)
df
Out[6]:
In [7]:
df['B']
Out[7]:
In [8]:
df.loc['a']
Out[8]:
In [9]:
df.iloc[1]
Out[9]:
In [10]:
df.ix[:,0]
Out[10]:
In [11]:
df.loc['a','B']
Out[11]:
In [12]:
df.iloc[0,1]
Out[12]:
In [13]:
df.ix['a',1]
Out[13]:
In [14]:
df.at['a','C']
Out[14]:
In [15]:
df.iat[0,2]
Out[15]:
In [16]:
df.iloc[:,0:3]
Out[16]:
In [17]:
df.iloc[::2,:]
Out[17]:
In [18]:
df[['A','C']]>10
Out[18]:
In [19]:
df_g2 = df[df[['A','B']]>2]
df_g2
Out[19]:
In [20]:
df_g2.dropna( axis=0, how='all')
Out[20]:
In [21]:
df_g2.dropna( axis=1, how='any')
Out[21]:
In [22]:
df[ df["A"].isin([1, 11]) ]
Out[22]:
In [23]:
df.values
Out[23]:
In [24]:
df.sort(columns="A", ascending=False)
Out[24]:
In [26]:
df2 = pd.DataFrame( np.random.rand(3,5), columns=df.columns )
pd.concat( [df, df2])
Out[26]:
In [27]:
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
Out[27]:
In [29]:
diamonds = pd.read_csv('diamonds.csv',index_col=0)
In [30]:
diamonds.head()
Out[30]:
In [31]:
diamonds.tail()
Out[31]:
In [73]:
diamonds.info()
In [74]:
diamonds.describe()
Out[74]:
In [75]:
diamonds.corr()
Out[75]:
In [76]:
diamonds.groupby("cut").mean()
Out[76]:
In [77]:
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"])
Out[77]:
In [78]:
diamonds.plot(x="carat", y="price", kind='scatter')
Out[78]:
In [79]:
diamonds["price"].plot(kind="hist")
Out[79]:
In [80]:
diamonds.groupby("cut")["price"].sum().plot(kind="barh")
Out[80]:
In [81]:
price_per_color = diamonds.groupby( ["cut", "color"] )["price"].sum().unstack()
price_per_color.plot(kind="barh", stacked=True)
#price_per_color
Out[81]: