美文网首页
Pandas练习笔记(一)----series

Pandas练习笔记(一)----series

作者: 香奈儿杨树林 | 来源:发表于2020-02-11 21:09 被阅读0次

pandas series基本知识

import pandas as pd
df = pd.read_csv("abc.csv")
1.Pandas series

关键字:一维对象

import pandas as pd
groceries = pd.Series(data = [30, 6, 'yes', 'no'], index=['eggs', 'apples', 'milk', 'bread'])
print(groceries)
eggs       30
apples    6
milk       Yes
bread     No
dtype: object

可以看出 Pandas Series 的显示方式为:第一列是索引,第二列是数据

2.获取series的属性:
print('Groceries has shape:', groceries.shape)
print('Groceries has dimension:', groceries.ndim)
print('Groceries has a total of', groceries.size, 'elements')

输出结果

Groceries has shape: (4,)
Groceries has dimension: 1
Groceries has a total of 4 elements
3.获取series的索引和数据:
# We print the index and data of Groceries
print('The data in Groceries is:', groceries.values)
print('The index of Groceries is:', groceries.index)

输出结果:

The data in Groceries is: [30 6 'Yes' 'No']
The index of Groceries is: Index(['eggs', 'apples', 'milk', 'bread'], dtype='object')
4.查询值是否在series内
x = 'bananas' in groceries
y = 'bread' in groceries
print('Is bananas an index label in Groceries:', x)
print('Is bread an index label in Groceries:', y)

输出结果:

Is bananas an index label in Groceries: False
Is bread an index label in Groceries: True

相关文章

网友评论

      本文标题:Pandas练习笔记(一)----series

      本文链接:https://www.haomeiwen.com/subject/hvdffhtx.html