Some examples/snippets of pandas use

https://jeffdelaney.me/blog/useful-snippets-in-pandas/

df = pd.read_csv('pizza.csv')
df = pd.read_csv('pizza.csv', parse_dates=['dates'])
df = pd.read_csv('pizza.csv', usecols=['foo', 'bar'])
df.head()       # first five rows
df.tail()       # last five rows
df.sample(5)    # random sample of rows
df.shape        # number of rows/columns in a tuple
df.describe()   # calculates measures of central tendency
df.info()       # memory footprint and datatypes

df['new_column'] = 23
full_price = (df.price + df.discount)
df['original_price'] = full_price
df.insert(0, 'original_price', full_price)

df.ix[2, 'topping']
df.topping.ix[2]
filtered_data = df[df.topping == 'pineapple']filtered_data = df[df.price > 11.99 ]
filtered_data = df[(df.price > 11.99) & (df.topping == 'Pineapple')]
df.sort_values('price', axis=0, ascending=False)
def calculate_taxes(price):
    taxes = price * 0.12
    return taxes

df['taxes'] = df.price.apply(calculate_taxes)


df['profitable'] = np.where(df['price']>=15.00, True, False)
df['mean'] = df.mean(axis=1)
or to find the standard deviation vertically

df.std(axis=0)

Comments

Popular posts from this blog

Tumblr posts about sexplanation, DoeEyes and Dr. Lindsay Doe

बैगन

Type of Common Charts used in visualization