In [1]:
import numpy as np
from matplotlib import pyplot as plt
plt.style.use('ggplot')
%matplotlib inline
In [2]:
import pandas as pd
In [3]:
A = np.array([[1,2,3,4,5], [10,20,30,40,50], [11,22,33,44,55]])
A
Out[3]:
In [4]:
index = ['a','b','c']
cols = ['A','B','C','D','E']
In [22]:
df = pd.DataFrame(data=A, index=index, columns=cols)
df
Out[22]:
In [8]:
df['B']
Out[8]:
In [9]:
df.loc['a']
Out[9]:
In [10]:
df.iloc[1]
Out[10]:
In [31]:
df.ix['a',1]
Out[31]:
In [29]:
df.loc['a','B']
Out[29]:
In [30]:
df.iloc[0,1]
Out[30]:
In [32]:
df.at['a','C']
Out[32]:
In [33]:
df.iat[0,2]
Out[33]:
In [36]:
df.iloc[:,0:3]
Out[36]:
In [37]:
df.iloc[::2,:]
Out[37]:
In [19]:
df[['A','C']]>10
Out[19]:
In [38]:
df_g2 = df[df[['A','B']]>2]
df_g2
Out[38]:
In [41]:
df_g2.dropna( axis=0, how='all')
Out[41]:
In [42]:
df_g2.dropna( axis=1, how='any')
Out[42]:
In [43]:
df[ df["A"].isin([1, 11]) ]
Out[43]:
In [44]:
df.values
Out[44]:
In [46]:
df.sort_values(by="A", ascending=False)
Out[46]:
In [50]:
df2 = pd.DataFrame( np.random.rand(3,5), columns=df.columns )
pd.concat( [df, df2])
Out[50]:
In [53]:
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[53]:
In [60]:
diamonds = pd.read_csv('diamonds.csv',index_col=0)
In [61]:
diamonds.head()
Out[61]:
In [62]:
diamonds.tail()
Out[62]:
In [63]:
diamonds.info()
In [64]:
diamonds.describe()
Out[64]:
In [65]:
diamonds.corr()
Out[65]:
In [69]:
diamonds.groupby("cut").mean()
Out[69]:
In [70]:
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[70]:
In [71]:
diamonds.plot(x="carat", y="price", kind='scatter')
Out[71]:
In [72]:
diamonds["price"].plot(kind="hist")
Out[72]:
In [77]:
diamonds.groupby("cut")["price"].sum().plot(kind="barh")
Out[77]:
In [40]:
price_per_color = diamonds.groupby( ["cut", "color"] )["price"].sum().unstack()
price_per_color.plot(kind="barh", stacked=True)
#price_per_color
Out[40]:
In [81]:
price_per_color = diamonds.groupby( ["cut", "color"] )["price"].sum().unstack()
price_per_color
Out[81]:
In [ ]: