第十五章 pandas官方文档0.22中文教程---Tutorials(lessons 4 lessons 5),个人渣翻译

这里写图片描述
####Lesson 4 --添加/删除列-索引操作
在这节课中我们将回到基础。我们将使用一个小的数据集,这样您就可以很容易地理解我要解释的内容。我们将添加列、删除列,并以许多不同的方式分割数据。享受吧!

import pandas as pd
# Our small data set
d = [0,1,2,3,4,5,6,7,8,9]

# Create dataframe
df = pd.DataFrame(d)
df

这里写图片描述

# Lets change the name of the column
df.columns = ['Rev']
df
# Lets add a column
df['NewCol'] = 5
df

这里写图片描述

# Lets modify our new column
df['NewCol'] = df['NewCol'] + 1
df

这里写图片描述

# We can delete columns
del df['NewCol']
df

这里写图片描述

# Lets add a couple of columns
df['test'] = 3
df['col'] = df['Rev']
df

这里写图片描述

# If we wanted, we could change the name of the index
i = ['a','b','c','d','e','f','g','h','i','j']
df.index = i
df

这里写图片描述
现在我们可以开始使用loc选择dataframe的片段。

df.loc['a']

这里写图片描述

# df.loc[inclusive:inclusive]
df.loc['a':'d']

这里写图片描述
我们还可以选择使用列名

df['Rev']
df[['Rev', 'test']]

这里写图片描述

df.loc[df.index[0:3],'Rev']

这里写图片描述


#####Lessons 5 Stack/Unstack/转置函数

# Our small data set
d = {'one':[1,1],'two':[2,2]}
i = ['a','b']

# Create dataframe
df = pd.DataFrame(data = d, index = i)
df

这里写图片描述

df.index

这里写图片描述

stack = df.stack()
stack

这里写图片描述

# The index now includes the column names
stack.index

这里写图片描述

unstack = df.unstack()
unstack

这里写图片描述
(列索引变成行索引,层次不一样。)
这里写图片描述
我们还可以使用T(转置)函数来使用索引来翻转列名

transpose = df.T
transpose

这里写图片描述

transpose.index

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值