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
网友评论