Pandas 学习工具及入门案例

Jupyter notebook 是学习 Pandas 的一个非常好用的工具。是一个交互式笔记本,支持 40 多种编程语言。
下面链接是简单的安装和使用教程https://www.jianshu.com/p/91365f343585。
以下是 Pandas 初步入门代码,大家可以尝试练习:

from pandas import DataFrame, read_csv

# General syntax to import a library but no functions: 
##import (library) as (give the library a nickname/alias)
import matplotlib.pyplot as plt
import pandas as pd #this is how I usually import pandas
import sys #only needed to determine Python version number
import matplotlib #only needed to determine Matplotlib version number

# Enable inline plotting
%matplotlib inline

print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
print('Matplotlib version ' + matplotlib.__version__)

Python version 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
Pandas version 0.23.4
Matplotlib version 2.2.2
names = ['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]
BabyDataSet = list(zip(names,births))
BabyDataSet
`[('Bob', 968), ('Jessica', 155), ('Mary', 77), ('John', 578), ('Mel', 973)]`
df = pd.DataFrame(data = BabyDataSet, columns = ['Names', 'Births'])
df

Pandas 学习工具及入门案例

df.to_csv('C:/Users/ThinkPad/Desktop/a.csv',index=False,header=False)
df

Pandas 学习工具及入门案例

df = pd.read_csv(Location, header=None)
df

Pandas 学习工具及入门案例

df = pd.read_csv(Location, names = ['Names', 'Births'])
df

Pandas 学习工具及入门案例

sorted = df.sort_values(['Births'], ascending=False)
sorted.head(1)

Pandas 学习工具及入门案例

# Create graph
df['Births'].plot()

# Maximum value in the data set
MaxValue = df['Births'].max()

# Name associated with the maximum value
MaxName = df['Names'][df['Births'] == df['Births'].max()].values

# Text to display on graph
Text = str(MaxValue) + " - " + MaxName

# Add text to graph
plt.annotate(Text, xy=(1, MaxValue), xytext=(8, 0), 
                 xycoords=('axes fraction', 'data'), textcoords='offset points')

print("The most popular name")
df[df['Births'] == df['Births'].max()]
#Sorted.head(1) can also be used

Pandas 学习工具及入门案例

下面是 Pandas 教学网址,大家可以学习和练习:
https://pandas.pydata.org/pandas-docs/stable/tutorials.html