Puzzle 21 What’s the Points?

Pandas Brain Teasers — by Miki Tebeka (29 / 34)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 It’s a Date! | TOC | Find Me a Phone Booth 👉

points.py

​ ​import​ ​pandas​ ​as​ ​pd​

​ df = pd.DataFrame([[1, 2], [3, 4]], columns=[​'x'​, ​'y'​])
​ ​print​(df.to_csv())

Guess the Output

IMPORTANT

Try to guess what the output is before moving to the next page.

images/hline.png

This code will print:

​ ,x,y
​ 0,1,2
​ 1,3,4</code></pre></td>
images/hline.png

What’s with the unnamed column that has 0 and 1 values?

The pandas.DataFrame documentation says:

Data structure also contains labeled axes (rows and columns). Arithmetic operations align on both row and column labels. Can be thought of as a dict-like container for Series objects. The primary pandas data structure.

The labeled axis for rows is called the index.

When you convert a pandas.DataFrame to another format (e.g., CSV, SQL, …), it will add the index by default.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.