Pandas是入门Python做数据分析必须要掌握的一个库,是一个开放源码、BSD 许可的库,提供高性能、易于使用的数据结构和数据分析的工具。主要数据结构是 Series (一维数据)与 DataFrame(二维数据),这两种数据结构足以处理金融、统计、社会科学、工程等领域里的大多数典型用例。今天就来一起学习。
# 运行以下代码
import pandas as pd
import datetime
import pandas as pd
# 运行以下代码
path6 = "../input/pandas_exercise/pandas_exercise/exercise_data/wind.data" # wind.data
import datetime
# 运行以下代码
data = pd.read_table(path6, sep = "\s+", parse_dates = [[0,1,2]])
data.head()
# 运行以下代码
def fix_century(x):
year = x.year - 100 if x.year > 1989 else x.year
return datetime.date(year, x.month, x.day)
# apply the function fix_century on the column and replace the values to the right ones
data['Yr_Mo_Dy'] = data['Yr_Mo_Dy'].apply(fix_century)
# data.info()
data.head()
# 运行以下代码
# transform Yr_Mo_Dy it to date type datetime64
data["Yr_Mo_Dy"] = pd.to_datetime(data["Yr_Mo_Dy"])
# set 'Yr_Mo_Dy' as the index
data = data.set_index('Yr_Mo_Dy')
data.head()
# data.info()
# 运行以下代码
data.isnull().sum()
# 运行以下代码
data.shape[0] - data.isnull().sum()
# 运行以下代码
data.mean().mean()
10.227982360836924
# 运行以下代码
loc_stats = pd.DataFrame()
loc_stats['min'] = data.min() # min
loc_stats['max'] = data.max() # max
loc_stats['mean'] = data.mean() # mean
loc_stats['std'] = data.std() # standard deviations
loc_stats
# 运行以下代码
# create the dataframe
day_stats = pd.DataFrame()
# this time we determine axis equals to one so it gets each row.
day_stats['min'] = data.min(axis = 1) # min
day_stats['max'] = data.max(axis = 1) # max
day_stats['mean'] = data.mean(axis = 1) # mean
day_stats['std'] = data.std(axis = 1) # standard deviations
day_stats.head()
注意:1961年的1月和1962年的1月应该区别对待
# 运行以下代码
# creates a new column 'date' and gets the values from the index
data['date'] = data.index
# creates a column for each value from date
data['month'] = data['date'].apply(lambda date: date.month)
data['year'] = data['date'].apply(lambda date: date.year)
data['day'] = data['date'].apply(lambda date: date.day)
# gets all value from the month 1 and assign to janyary_winds
january_winds = data.query('month == 1')
# gets the mean from january_winds, using .loc to not print the mean of month, year and day
january_winds.loc[:,'RPT':"MAL"].mean()
# 运行以下代码
data.query('month == 1 and day == 1')
# 运行以下代码
data.query('day == 1')
# 运行以下代码
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
%matplotlib inline
# 运行以下代码
path7 = '../input/pandas_exercise/pandas_exercise/exercise_data/train.csv' # train.csv
# 运行以下代码
titanic = pd.read_csv(path7)
titanic.head()
# 运行以下代码
titanic.set_index('PassengerId').head()
# 运行以下代码
# sum the instances of males and females
males = (titanic['Sex'] == 'male').sum()
females = (titanic['Sex'] == 'female').sum()
# put them into a list called proportions
proportions = [males, females]
# Create a pie chart
plt.pie(
# using proportions
proportions,
# with the labels being officer names
labels = ['Males', 'Females'],
# with no shadows
shadow = False,
# with colors
colors = ['blue','red'],
# with one slide exploded out
explode = (0.15 , 0),
# with the start angle at 90%
startangle = 90,
# with the percent listed as a fraction
autopct = '%1.1f%%'
)
# View the plot drop above
plt.axis('equal')
# Set labels
plt.title("Sex Proportion")
# View the plot
plt.tight_layout()
plt.show()
# 运行以下代码
# creates the plot using
lm = sns.lmplot(x = 'Age', y = 'Fare', data = titanic, hue = 'Sex', fit_reg=False)
# set title
lm.set(title = 'Fare x Age')
# get the axes object and tweak it
axes = lm.axes
axes[0,0].set_ylim(-5,)
axes[0,0].set_xlim(-5,85)
(-5, 85)
# 运行以下代码
titanic.Survived.sum()
342
# 运行以下代码
# sort the values from the top to the least value and slice the first 5 items
df = titanic.Fare.sort_values(ascending = False)
df
# create bins interval using numpy
binsVal = np.arange(0,600,10)
binsVal
# create the plot
plt.hist(df, bins = binsVal)
# Set the title and labels
plt.xlabel('Fare')
plt.ylabel('Frequency')
plt.title('Fare Payed Histrogram')
# show the plot
plt.show()
# 运行以下代码
import pandas as pd
# 运行以下代码
raw_data = {
"name": ['Bulbasaur', 'Charmander','Squirtle','Caterpie'],
"evolution": ['Ivysaur','Charmeleon','Wartortle','Metapod'],
"type": ['grass', 'fire', 'water', 'bug'],
"hp": [45, 39, 44, 45],
"pokedex": ['yes', 'no','yes','no']
}
# 运行以下代码
pokemon = pd.DataFrame(raw_data)
pokemon.head()
# 运行以下代码
pokemon = pokemon[['name', 'type', 'hp', 'evolution','pokedex']]
pokemon
# 运行以下代码
pokemon['place'] = ['park','street','lake','forest']
pokemon
# 运行以下代码
pokemon.dtypes
# 运行以下代码
import pandas as pd
import numpy as np
# visualization
import matplotlib.pyplot as plt
%matplotlib inline
# 运行以下代码
path9 = '../input/pandas_exercise/pandas_exercise/exercise_data/Apple_stock.csv' # Apple_stock.csv
# 运行以下代码
apple = pd.read_csv(path9)
apple.head()
# 运行以下代码
apple.dtypes
# 运行以下代码
apple.Date = pd.to_datetime(apple.Date)
apple['Date'].head()
# 运行以下代码
apple = apple.set_index('Date')
apple.head()
# 运行以下代码
apple.index.is_unique
True
# 运行以下代码
apple.sort_index(ascending = True).head()
# 运行以下代码
apple_month = apple.resample('BM')
apple_month.head()
# 运行以下代码
(apple.index.max() - apple.index.min()).days
12261
# 运行以下代码
apple_months = apple.resample('BM').mean()
len(apple_months.index)
404
# 运行以下代码
# makes the plot and assign it to a variable
appl_open = apple['Adj Close'].plot(title = "Apple Stock")
# changes the size of the graph
fig = appl_open.get_figure()
fig.set_size_inches(13.5, 9)
# 运行以下代码
import pandas as pd
# 运行以下代码
path10 ='../input/pandas_exercise/pandas_exercise/exercise_data/iris.csv' # iris.csv
# 运行以下代码
iris = pd.read_csv(path10)
iris.head()
iris = pd.read_csv(path10,names = ['sepal_length','sepal_width', 'petal_length', 'petal_width', 'class'])
iris.head()
# 运行以下代码
pd.isnull(iris).sum()
# 运行以下代码
iris.iloc[10:20,2:3] = np.nan
iris.head(20)
# 运行以下代码
iris.petal_length.fillna(1, inplace = True)
iris
# 运行以下代码
del iris['class']
iris.head()
# 运行以下代码
iris.iloc[0:3 ,:] = np.nan
iris.head()
# 运行以下代码
iris = iris.dropna(how='any')
iris.head()
# 运行以下代码
iris = iris.reset_index(drop = True)
iris.head()
你们的支持是我持续更新下去的动力,(点赞,关注,评论) 这篇文还未结束,想了解后续的可以关注我,持续更新。
点击领取 Q群号: 943192807(纯技术交流和资源共享)以自助拿走。
①行业咨询、专业解答 ②Python开发环境安装教程 ③400集自学视频 ④软件开发常用词汇 ⑤最新学习路线图 ⑥3000多本Python电子书