Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 16658
%auto import pandas as pd import numpy as np import matplotlib.pyplot as plt
%auto # so that numbers are Python by default, not sage, which would confuse pandas. %default_mode python # make all output look nice, e.g., html tables, without havig to explicitly type show. %typeset_mode True
# create a Series with an arbitrary list s = pd.Series([7, 'Heisenberg', 3.14, -1789710578, 'Happy Eating!']) s
0 7 1 Heisenberg 2 3.14 3 -1789710578 4 Happy Eating! dtype: object
# Arbitrary index s = pd.Series([7, 'Heisenberg', 3.14, -1789710578, 'Happy Eating!'], index=['A', 'Z', 'C', 'Y', 'E']) s
A 7 Z Heisenberg C 3.14 Y -1789710578 E Happy Eating! dtype: object
d = {'Chicago': 1000, 'New York': 1300, 'Portland': 900, 'San Francisco': 1100, 'Austin': 450, 'Boston': None} cities = pd.Series(d) cities
Austin 450.0 Boston NaN Chicago 1000.0 New York 1300.0 Portland 900.0 San Francisco 1100.0 dtype: float64
(cities < 1000)
Austin True Boston False Chicago False New York False Portland True San Francisco False dtype: bool
cities[cities < 1000]
Austin 450.0 Portland 900.0 dtype: float64
cities[cities < 1000] = 750 cities[cities < 1000] print cities
Austin 750.0 Portland 750.0 dtype: float64
Austin 750.0 Boston NaN Chicago 1000.0 New York 1300.0 Portland 750.0 San Francisco 1100.0 dtype: float64
"Austin" in cities "Seattle" in cities
True\displaystyle \mathrm{True}
False\displaystyle \mathrm{False}
cities["Seattle"] = 200 print "Seattle" in cities print cities
True Austin 750.0 Boston NaN Chicago 1000.0 New York 1300.0 Portland 750.0 San Francisco 1100.0 Seattle 200.0 dtype: float64

Dataframes

data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012], 'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'], 'wins': [11, 8, 10, 15, 11, 6, 10, 4], 'losses': [5, 8, 6, 1, 5, 10, 6, 12]} football = pd.DataFrame(data, columns=['year', 'team', 'wins', 'losses']) football
year team wins losses
0 2010 Bears 11 5
1 2011 Bears 8 8
2 2012 Bears 10 6
3 2011 Packers 15 1
4 2012 Packers 11 5
5 2010 Lions 6 10
6 2011 Lions 10 6
7 2012 Lions 4 12
football.to_excel('football.xlsx', index=False)
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/pandas/core/frame.py", line 1414, in to_excel excel_writer = ExcelWriter(excel_writer, engine=engine) File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/pandas/io/excel.py", line 713, in __init__ if not openpyxl_compat.is_compat(major_ver=self.openpyxl_majorver): File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/pandas/compat/openpyxl_compat.py", line 27, in is_compat import openpyxl ImportError: No module named openpyxl
url = 'https://raw.github.com/gjreda/best-sandwiches/master/data/best-sandwiches-geocode.tsv' # fetch the text from the URL and read it into a DataFrame from_url = pd.read_table(url, sep='\t') from_url.head(3)
rank sandwich restaurant description price address city phone website full_address formatted_address lat lng
0 1 BLT Old Oak Tap The B is applewood smoked—nice and snapp... $10 2109 W. Chicago Ave. Chicago 773-772-0406 theoldoaktap.com 2109 W. Chicago Ave., Chicago 2109 West Chicago Avenue, Chicago, IL 60622, USA 41.895734 -87.679960
1 2 Fried Bologna Au Cheval Thought your bologna-eating days had retired w... $9 800 W. Randolph St. Chicago 312-929-4580 aucheval.tumblr.com 800 W. Randolph St., Chicago 800 West Randolph Street, Chicago, IL 60607, USA 41.884672 -87.647754
2 3 Woodland Mushroom Xoco Leave it to Rick Bayless and crew to come up w... $9.50. 445 N. Clark St. Chicago 312-334-3688 rickbayless.com 445 N. Clark St., Chicago 445 North Clark Street, Chicago, IL 60654, USA 41.890602 -87.630925
︠2cbc9700-d027-463f-9b95-b18ebd90289b︠