Since many potential pandas users have some familiarity with spreadsheet programs like
Excel, this page is meant to provide some examples
of how various spreadsheet operations would be performed using pandas. This page will use
terminology and link to documentation for Excel, but much will be the same/similar in
Google Sheets,
LibreOffice Calc,
Apple Numbers, and other
Excel-compatible spreadsheet software.
If you’re new to pandas, you might want to first read through 10 Minutes to pandas
to familiarize yourself with the library.
As is customary, we import pandas and NumPy as follows:
In [1]: importpandasas
pd
In [2]:
import
numpy
as
np
Data structures
General terminology translation
pandas
Excel
DataFrame
worksheet
Series
column
Index
row headings
row
row
NaN
empty cell
DataFrame
A DataFrame in pandas is analogous to an Excel worksheet. While an Excel workbook can contain
multiple worksheets, pandas DataFrames exist independently.
Series
A Series is the data structure that represents one column of a DataFrame. Working with a
Series is analogous to referencing a column of a spreadsheet.
Index
Every DataFrame and Series has an Index, which are labels on the rows of the data. In
pandas, if no index is specified, a RangeIndex is used by default (first row = 0,
second row = 1, and so on), analogous to row headings/numbers in spreadsheets.
In pandas, indexes can be set to one (or multiple) unique values, which is like having a column that
is used as the row identifier in a worksheet. Unlike most spreadsheets, these Index values can
actually be used to reference the rows. (Note that this can be done in Excel with structured
references.)
For example, in spreadsheets, you would reference the first row as A1:Z1, while in pandas you
could use .
Copies vs. in place operations
Most pandas operations return copies of the Series/DataFrame. To make the changes “stick”,
you’ll need to either assign to a new variable:
A pandas DataFrame can be constructed in many different ways,
but for a small number of values, it is often convenient to specify it as
a Python dictionary, where the keys are the column names
and the values are the data.
In [3]: df=pd.DataFrame({"x":[1,3,5],"y":[2,4,6]})In [4]: dfOut[4]: x y0 1 21 3 42 5 6
Reading external data
Both Excel
and pandas can import data from various sources in various
formats.
CSV
Let’s load and display the tips
dataset from the pandas tests, which is a CSV file. In Excel, you would download and then
open the CSV.
In pandas, you pass the URL or local path of the CSV file to read_csv():
In [5]: url=( ...: "https://raw.githubusercontent.com/pandas-dev"
Limiting output
Spreadsheet programs will only show one screenful of data at a time and then allow you to scroll, so
there isn’t really a need to limit output. In pandas, you’ll need to put a little more thought into
controlling how your DataFrames are displayed.
In spreadsheets, formulas
are often created in individual cells and then dragged
into other cells to compute them for other columns. In pandas, you’re able to do operations on whole
columns directly.
pandas provides vectorized operations by specifying the individual Series in the
DataFrame. New columns can be assigned in the same way. The DataFrame.drop() method drops
a column from the DataFrame.
In [9]: tips["total_bill"]=tips["total_bill"]-2In [10]: tips["new_bill"]=tips["total_bill"]/2In [11]: tipsOut[11]: total_bill tip sex smoker day time size new_bill0 14.99 1.01 Female No Sun Dinner 2 7.4951 8.34 1.66 Male No Sun Dinner 3 4.1702 19.01 3.50 Male No Sun Dinner 3 9.5053 21.68 3.31 Male No Sun Dinner 2 10.8404 22.59 3.61 Female No Sun Dinner 4 11.295.. ... ... ... ... ... ... ... ...239 27.03 5.92 Male No Sat Dinner 3 13.515240 25.18 2.00 Female Yes Sat Dinner 2 12.590241 20.67 2.00 Male Yes Sat Dinner 2 10.335242 15.82 1.75 Male No Sat Dinner 2 7.910243 16.78 3.00 Female No Thur Dinner 2 8.390[244 rows x 8 columns]In [12]: tips=tips.drop("new_bill",axis=1)
DataFrames can be filtered in multiple ways; the most intuitive of which is using
boolean indexing.
In [13]: tips[tips["total_bill"]>10]Out[13]: total_bill tip sex smoker day time size0 14.99 1.01 Female No Sun Dinner 22 19.01 3.50 Male No Sun Dinner 33 21.68 3.31 Male No Sun Dinner 24 22.59 3.61 Female No Sun Dinner 45 23.29 4.71 Male No Sun Dinner 4
If/then logic
Let’s say we want to make a bucket column with values of low and high, based on whether
the total_bill is less or more than $10.
In spreadsheets, logical comparison can be done with conditional formulas.
We’d use a formula of =IF(A2<10,"low","high"), dragged to all cells in a new bucket
column.
The same operation in pandas can be accomplished using
the where method from .
Date functionality
This section will refer to “dates”, but timestamps are handled similarly.
We can think of date functionality in two parts: parsing, and output. In spreadsheets, date values
are generally parsed automatically, though there is a DATEVALUE
function if you need it. In pandas, you need to explicitly convert plain text to datetime objects,
either while reading from a CSV or once in a DataFrame.
Once parsed, spreadsheets display the dates in a default format, though the format can be changed.
In pandas, you’ll generally want to keep dates as datetime objects while you’re doing
calculations with them. Outputting parts of dates (such as the year) is done through date
functions
in spreadsheets, and datetime properties in pandas.
Given and in columns and of a spreadsheet, you might have these
formulas:
Selection of columns
In spreadsheets, you can select columns you want by:
In [30]: tips=tips.sort_values(["sex","total_bill"])In [31]: tipsOut[31]: total_bill tip sex smoker day time size67 1.07 1.00 Female Yes Sat Dinner 1
String processing
Finding length of string
In spreadsheets, the number of characters in text can be found with the LEN
function. This can be used with the TRIM
function to remove extra whitespace.
=LEN(TRIM(A2))
You can find the length of a character string with Series.str.len().
In Python 3, all strings are Unicode strings. len includes trailing blanks.
Use len and rstrip to exclude trailing blanks.
Note this will still include multiple spaces within the string, so isn’t 100% equivalent.
Finding position of substring
The FIND
spreadsheet function returns the position of a substring, with the first character being 1.
You can find the position of a character in a column of strings with the Series.str.find()
method. find searches for the first position of the substring. If the substring is found, the
method returns its position. If not found, it returns -1. Keep in mind that Python indexes are
zero-based.
In [34]: tips["sex"]
Extracting substring by position
Spreadsheets have a MID
formula for extracting a substring from a given position. To get the first character:
=MID(A2,1,1)
With pandas you can use [] notation to extract a substring
from a string by position locations. Keep in mind that Python
indexes are zero-based.
The simplest way to extract words in pandas is to split the strings by spaces, then reference the
word by index. Note there are more powerful approaches should you need them.
In [36]: firstlast=pd.DataFrame({"String":["John Smith","Jane Cook"]})In [37]: firstlast["First_Name"]=firstlast
The following tables will be used in the merge examples:
In [45]: df1=pd.DataFrame({"key":["A","B","C","D"],"value":np.random.randn(4)})In [46]: df1Out[46]: key value0 A 0.4691121 B -0.2828632 C -1.5090593 D -1.135632In [47]: df2=pd.DataFrame({"key":["B","D","D","E"],"value":np.random.randn(4)})In [48]: df2Out[48]: key value0 B 1.2121121 D -0.1732152 D 0.1192093 E -1.044236
pandas DataFrames have a merge() method, which provides similar functionality. The
data does not have to be sorted ahead of time, and different join types are accomplished via the
how keyword.
In [49]: inner_join=df1.merge(df2,on=["key"],how="inner")In [50]: inner_joinOut[50]: key value_x value_y0 B -0.282863 1.2121121 D -1.135632 -0.1732152 D -1.135632 0.119209In [51]: left_join=df1.merge(df2,on=
merge has a number of advantages over VLOOKUP:
The lookup value doesn’t need to be the first column of the lookup table
If multiple rows are matched, there will be one row for each match, instead of just the first
It will include all columns from the lookup table, instead of just a single specified column
Create a series of numbers following a set pattern in a certain set of cells. In
a spreadsheet, this would be done by shift+drag after entering the first number or by
entering the first two or three values and then dragging.
This can be achieved by creating a series and assigning it to the desired cells.
In [63]: df=pd.DataFrame( ....: { ....: "class":["A","A","A","B",
Pivot Tables
PivotTables
from spreadsheets can be replicated in pandas through Reshaping and pivot tables. Using the tips dataset again,
let’s find the average gratuity by size of the party and sex of the server.
In Excel, we use the following configuration for the PivotTable:
The equivalent in pandas:
In [66]: pd.pivot_table( ....: tips,values="tip",index=
Adding a row
Assuming we are using a RangeIndex (numbered 0, 1, etc.), we can use concat() to add a row to the bottom of a DataFrame.
In [67]: dfOut[67]: class student_count all_pass0 A 42 Yes1 A 35 Yes2 A 42 Yes3 B 50 No
Find and Replace
Excel’s Find dialog
takes you to cells that match, one by one. In pandas, this operation is generally done for an
entire column or DataFrame at once through conditional expressions.
In [70]: tipsOut[70]: total_bill tip sex smoker day time size67 1.07 1.00 Female Yes Sat Dinner 192 3.75 1.00 Female Yes Fri Dinner 2111 5.25 1.00 Female No Sat Dinner 1145 6.35 1.50 Female No Thur Lunch 2135 6.51 1.25 Female No Thur Lunch 2.. ... ... ... ... ... ... ...182 43.35 3.50 Male Yes Sun Dinner 3156 46.17 5.00 Male No Sun Dinner 659 46.27 6.73 Male No Sat Dinner 4
populations.loc['Chicago']
Index values are also persistent, so if you re-order the rows in a DataFrame, the label for a
particular row don’t change.
Like Excel’s Text Import Wizard,
read_csv can take a number of parameters to specify how the data should be parsed. For
example, if the data was instead tab delimited, and did not have column names, the pandas command
would be:
tips=pd.read_csv("tips.csv",sep="\t",header=None)# alternatively, read_table is an alias to read_csv with tab delimitertips=pd.read_table("tips.csv",header=None)
Should you wish to subsequently access the data in the tips.xlsx file, you can read it into your module using
tips_df=pd.read_excel("./tips.xlsx",index_col=0)
You have just read in an Excel file using pandas!
5
)
Out[8]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
.. ... ... ... ... ... ... ...
239 27.03 5.92 Male No Sat Dinner 3
240 25.18 2.00 Female Yes Sat Dinner 2
241 20.67 2.00 Male Yes Sat Dinner 2
242 15.82 1.75 Male No Sat Dinner 2
243 16.78 3.00 Female No Thur Dinner 2
[204 rows x 7 columns]
The above statement is simply passing a Series of True/False objects to the DataFrame,
returning all rows with True.
In [14]: is_dinner=tips["time"]=="Dinner"In [15]: is_dinnerOut[15]: 0 True1 True2 True3 True4 True ... 239 True240 True241 True242 True243 TrueName: time, Length: 244, dtype: boolIn [16]: is_dinner.value_counts()Out[16]: timeTrue 176False 68Name: count, dtype: int64In [17]: tips[is_dinner]Out[17]: total_bill tip sex smoker day time size0 14.99 1.01 Female No Sun Dinner 21 8.34 1.66 Male No Sun Dinner 32 19.01 3.50 Male No Sun Dinner 33 21.68 3.31 Male No Sun Dinner 24 22.59 3.61 Female No Sun Dinner 4.. ... ... ... ... ... ... ...239 27.03 5.92 Male No Sat Dinner 3240 25.18 2.00 Female Yes Sat Dinner 2241 20.67 2.00 Male Yes Sat Dinner 2242 15.82 1.75 Male No Sat Dinner 2243 16.78 3.00 Female No Thur Dinner 2[176 rows x 7 columns]
numpy
In [18]: tips["bucket"]=np.where(tips["total_bill"]<10,"low","high")In [19]: tipsOut[19]: total_bill tip sex smoker day time size bucket0 14.99 1.01 Female No Sun Dinner 2 high1 8.34 1.66 Male No Sun Dinner 3 low2 19.01 3.50 Male No Sun Dinner 3 high3 21.68 3.31 Male No Sun Dinner 2 high4 22.59 3.61 Female No Sun Dinner 4 high.. ... ... ... ... ... ... ... ...239 27.03 5.92 Male No Sat Dinner 3 high240 25.18 2.00 Female Yes Sat Dinner 2 high241 20.67 2.00 Male Yes Sat Dinner 2 high242 15.82 1.75 Male No Sat Dinner 2 high243 16.78 3.00 Female No Thur Dinner 2 high[244 rows x 8 columns]
In [28]: tips.drop("sex",axis=1)Out[28]: total_bill tip smoker day time size0 14.99 1.01 No Sun Dinner 21 8.34 1.66 No Sun Dinner 32 19.01 3.50 No Sun Dinner 33 21.68 3.31 No Sun Dinner 24 22.59 3.61 No Sun Dinner 4.. ... ... ... ... ... ...239 27.03 5.92 No Sat Dinner 3240 25.18 2.00 Yes Sat Dinner 2241 20.67 2.00 Yes Sat Dinner 2242 15.82 1.75 No Sat Dinner 2243 16.78 3.00 No Thur Dinner 2[244 rows x 6 columns]
Rename a column
In [29]: tips.rename(columns={"total_bill":"total_bill_2"})Out[29]: total_bill_2 tip sex smoker day time size0 14.99 1.01 Female No Sun Dinner 21 8.34 1.66 Male No Sun Dinner 32 19.01 3.50 Male No Sun Dinner 33 21.68 3.31 Male No Sun Dinner 24 22.59 3.61 Female No Sun Dinner 4.. ... ... ... ... ... ... ...239 27.03 5.92 Male No Sat Dinner 3240 25.18 2.00 Female Yes Sat Dinner 2241 20.67 2.00 Male Yes Sat Dinner 2242 15.82 1.75 Male No Sat Dinner 2243 16.78 3.00 Female No Thur Dinner 2[244 rows x 7 columns]
92 3.75 1.00 Female Yes Fri Dinner 2
111 5.25 1.00 Female No Sat Dinner 1
145 6.35 1.50 Female No Thur Lunch 2
135 6.51 1.25 Female No Thur Lunch 2
.. ... ... ... ... ... ... ...
182 43.35 3.50 Male Yes Sun Dinner 3
156 46.17 5.00 Male No Sun Dinner 6
59 46.27 6.73 Male No Sat Dinner 4
212 46.33 9.00 Male No Sat Dinner 4
170 48.81 10.00 Male Yes Sat Dinner 3
[244 rows x 7 columns]
len
()
Out[33]:
67 6
92 6
111 6
145 5
135 5
..
182 6
156 6
59 6
212 6
170 6
Name: time, Length: 244, dtype: int64
.
str
.
find
(
"ale"
)
Out[34]:
67 3
92 3
111 3
145 3
135 3
..
182 1
156 1
59 1
212 1
170 1
Name: sex, Length: 244, dtype: int64
0
:
1
]
Out[35]:
67 F
92 F
111 F
145 F
135 F
..
182 M
156 M
59 M
212 M
170 M
Name: sex, Length: 244, dtype: object
[
"String"
]
.
str
.
split
(
" "
,
expand
=
True
)[
0
]
In [38]:
firstlast
[
"Last_Name"
]
=
firstlast
[
"String"
]
.
str
.
rsplit
(
" "
,
expand
=
True
)[
1
]
In [39]:
firstlast
Out[39]:
String First_Name Last_Name
0 John Smith John Smith
1 Jane Cook Jane Cook
"string"
:
[
"John Smith"
,
"Jane Cook"
]})
In [41]:
firstlast
[
"upper"
]
=
firstlast
[
"string"
]
.
str
.
upper
()
In [42]:
firstlast
[
"lower"
]
=
firstlast
[
"string"
]
.
str
.
lower
()
In [43]:
firstlast
[
"title"
]
=
firstlast
[
"string"
]
.
str
.
title
()
In [44]:
firstlast
Out[44]:
string upper lower title
0 John Smith JOHN SMITH john smith John Smith
1 Jane Cook JANE COOK jane cook Jane Cook
[
"key"
],
how
=
"left"
)
In [52]:
left_join
Out[52]:
key value_x value_y
0 A 0.469112 NaN
1 B -0.282863 1.212112
2 C -1.509059 NaN
3 D -1.135632 -0.173215
4 D -1.135632 0.119209
In [53]:
right_join
=
df1
.
merge
(
df2
,
on
=
[
"key"
],
how
=
"right"
)
In [54]:
right_join
Out[54]:
key value_x value_y
0 B -0.282863 1.212112
1 D -1.135632 -0.173215
2 D -1.135632 0.119209
3 E NaN -1.044236
In [55]:
outer_join
=
df1
.
merge
(
df2
,
on
=
[
"key"
],
how
=
"outer"
)
In [56]:
outer_join
Out[56]:
key value_x value_y
0 A 0.469112 NaN
1 B -0.282863 1.212112
2 C -1.509059 NaN
3 D -1.135632 -0.173215
4 D -1.135632 0.119209
5 E NaN -1.044236
"C"
,
"D"
],
....:
"student_count"
:
[
42
,
35
,
42
,
50
,
47
,
45
],
....:
"all_pass"
:
[
"Yes"
,
"Yes"
,
"Yes"
,
"No"
,
"No"
,
"Yes"
],
....:
}
....:
)
....:
In [64]:
df
.
drop_duplicates
()
Out[64]:
class student_count all_pass
0 A 42 Yes
1 A 35 Yes
3 B 50 No
4 C 47 No
5 D 45 Yes
In [65]:
df
.
drop_duplicates
([
"class"
,
"student_count"
])
Out[65]:
class student_count all_pass
0 A 42 Yes
1 A 35 Yes
3 B 50 No
4 C 47 No
5 D 45 Yes
[
"size"
],
columns
=
[
"sex"
],
aggfunc
=
np
.
average
....:
)
....:
Out[66]:
sex Female Male
size
1 1.276667 1.920000
2 2.528448 2.614184
3 3.250000 3.476667
4 4.021111 4.172143
5 5.140000 3.750000
6 4.600000 5.850000
4 C 47 No
5 D 45 Yes
In [68]:
new_row
=
pd
.
DataFrame
([[
"E"
,
51
,
True
]],
....:
columns
=
[
"class"
,
"student_count"
,
"all_pass"
])
....:
In [69]:
pd
.
concat
([
df
,
new_row
])
Out[69]:
class student_count all_pass
0 A 42 Yes
1 A 35 Yes
2 A 42 Yes
3 B 50 No
4 C 47 No
5 D 45 Yes
0 E 51 True
212 46.33 9.00 Male No Sat Dinner 4
170 48.81 10.00 Male Yes Sat Dinner 3
[244 rows x 7 columns]
In [71]:
tips
==
"Sun"
Out[71]:
total_bill tip sex smoker day time size
67 False False False False False False False
92 False False False False False False False
111 False False False False False False False
145 False False False False False False False
135 False False False False False False False
.. ... ... ... ... ... ... ...
182 False False False False True False False
156 False False False False True False False
59 False False False False False False False
212 False False False False False False False
170 False False False False False False False
[244 rows x 7 columns]
In [72]:
tips
[
"day"
]
.
str
.
contains
(
"S"
)
Out[72]:
67 True
92 False
111 True
145 False
135 False
...
182 True
156 True
59 True
212 True
170 True
Name: day, Length: 244, dtype: bool
pandas’ replace() is comparable to Excel’s ReplaceAll.
In [73]: tips.replace("Thu","Thursday")Out[73]: total_bill tip sex smoker day time size67 1.07 1.00 Female Yes Sat Dinner 192 3.75 1.00 Female Yes Fri Dinner 2111 5.25 1.00 Female No Sat Dinner 1145 6.35 1.50 Female No Thur Lunch 2135 6.51 1.25 Female No Thur Lunch 2.. ... ... ... ... ... ... ...182 43.35 3.50 Male Yes Sun Dinner 3156 46.17 5.00 Male No Sun Dinner 659 46.27 6.73 Male No Sat Dinner 4212 46.33 9.00 Male No Sat Dinner 4170 48.81 10.00 Male Yes Sat Dinner 3[244 rows x 7 columns]