羟基的pandas自学(一)

Series的创建和一些用法:

使用pandas,第一步当然是安装pandas库啦,命令行输入:

pip install pandas

安装完毕后使用import pandas as pd即可以使用pandas库啦。

Series的创建:

sir = pd.Series([1, 2, 3, 4, 5])
print(sir)

运行结果:

0    1
1    2
2    3
3    4
dtype: int64

第一列为索引,第二列为数据,最后的int64是数据类型。

  • Series的默认索引为0~n,当然我们也可以自己添加索引,例如:
sir = pd.Series([1, 2, 3, 4], index = ['a', 'b', 'c', 'd'])
print(sir)

输出:

a    1
b    2
c    3
d    4
dtype: int64

索引变成了’a’, ‘b’, ‘c’, ‘d’

  • 到这里大家也看出来了,如果说numpy中的array像是数组的话,那么pandas中的Series就可以说是字典了。
    因此可以这样:
print(sir['b'])

结果显而易见:

>>> print(sir['b'])
2
  • 我们还可以这样创建Series:
dict = {'a':1, 'b':3, 'c':5.5, 'd':-1.3}
sir = pd.Series(dict)
print(sir) 

输出结果:

a    1.0
b    3.0
c    5.5
d   -1.3
dtype: float64

利用Series进行数据处理:

  • DataFrame
dict = {
		'courses':['english', 'python', 'c plus plus', 'Java', 'php'],
		'time':['1 hour', '2 hours', '3.5 hours', '2 hours', '1 hour'],
		'teacher':['Yu', 'ceng', 'wang', 'gu', 'peter']
		}
ser = pd.DataFrame(dict)
print(ser)

输出:(本来是一个表格,命令行里显示不出来//滑稽)

       courses teacher       time
0      english      Yu     1 hour
1       python    ceng    2 hours
2  c plus plus    wang  3.5 hours
3         Java      gu    2 hours
4          php   peter     1 hour

当然,貌似DataFrame是进行数据处理的,我们正儿八经的建个数据表格叭:

table = pd.DataFrame([[1000, 2000, 3000], 
					[900, 2100, 3500], 
					[940, 2300, 3100]], index = ['A plant', 'B plant', 'C plant'], 
					columns = ['sources', 'pay', 'price'])
print(table)

输出:

        sources   pay  price
A plant     1000  2000   3000
B plant      900  2100   3500
C plant      940  2300   3100

第一个参数为array(表格中的数据),第二个参数为index(索引),第三个参数为columns(第一行)

  • DataFrame的一些信息:
>>> print(table.index)
Index(['A plant', 'B plant', 'C plant'], dtype='object')
>>> print(table.columns)
Index(['sources', 'pay', 'price'], dtype='object')
>>> print(table.values)
[[1000 2000 3000]
 [ 900 2100 3500]
 [ 940 2300 3100]]
  • .describe()
>>> table.describe()

输出:

           sources          pay        price
count     3.000000     3.000000     3.000000
mean    946.666667  2133.333333  3200.000000
std      50.332230   152.752523   264.575131
min     900.000000  2000.000000  3000.000000
25%     920.000000  2050.000000  3050.000000
50%     940.000000  2100.000000  3100.000000
75%     970.000000  2200.000000  3300.000000
max    1000.000000  2300.000000  3500.000000
索引含义
count出现次数
mean平均数
std标准差
min最小值
max最大值
  • 转置:
    输入:
print(table.T)

输出:

         A plant  B plant  C plant
sources     1000      900      940
pay         2000     2100     2300
price       3000     3500     3100
  • 按索引排序:
print(table.sort_index(axis = 0)) #行排序
print(table.sort_index(axis = 1)) #列排序
  • 按值排序:例如
print(table.sort_values(by = 'price'))

输出:

         sources   pay  price
A plant     1000  2000   3000
C plant      940  2300   3100
B plant      900  2100   3500

Pandas进行数据提取:

  • 提取某几行或者某几列,利用索引提取table['souces']
    输出:
>>> table['sources']
A plant    1000
B plant     900
C plant     940
Name: sources, dtype: int64
  • 利用标签提取:
table.loc(array1, array2)

说明:array1为行的索引,array2为列的索引

  • 利用位置提取:
table.iloc(array1, array2)

说明:array1为行的位置,array2为列的位置

  • 混合提取:
table.ix(array1, array2)

此时array1, array2既可以为标签也可以为位置。

  • 来一个高级点的玩法://滑稽
# 引例:
>>> print(table.sources>900)
A plant     True
B plant    False
C plant     True
Name: sources, dtype: bool

我们可以通过true or false选择打印数据

>>> print(table[table.sources>900])
         sources   pay  price
A plant     1000  2000   3000
C plant      940  2300   3100

按往常惯例,明天继续更新。//呲牙

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值