区别:
Series,只是一个一维数据结构,它由index和value组成。
DataFrame,是一个二维结构,除了拥有index和value之外,还拥有column。
联系:
DataFrame由多个Series组成,无论是行还是列,单独拆分出来都是一个Series。
代码演示:
import pandas as pd
data = {'Country':['Belgium', 'India', 'Brazil'],
'Capital':['Brussels', 'New Delhi', 'Brasilia'],
'Population':[11190846, 1303171035, 207847528]
}
# Series
s1 = Series(data['Country'])
print(s1)
'''
0 Belgium
1 India
2 Brazil
dtype: object
'''
print(s1.values) # 类型: <class 'numpy.ndarray'>
'''
['Belgium' 'India' 'Brazil']
'''
print(s1.index)
'''
RangeIndex(start=0, stop=3, step=1)
'''
# 为Series指定index
s1 = Series(data['Country'], index=['A', 'B', 'C'])
print(s1)
''' 索引更改
A Belgium
B India
C Brazil
dtype: object
'''
# Dataframe
df1 = pd.DataFrame(data)
print(df1)
'''
Capital Country Population
0 Brussels Belgium 11190846
1 New Delhi India 1303171035
2 Brasilia Brazil 207847528
'''
print(df1['Capital']) # 类型: series
'''
0 Brussels
1 New Delhi
2 Brasilia
Name: Capital, dtype: object
'''