Series与DataFrame的区别
DataFrame是多个共用相同索引的Series组成
Series没有列索引,DataFrame有列索引
#coding=utf-8
import pandas as pd
import numpy as np
dicts={"name":["lemon","jack","jason"],
"age":[20,15,30],
"sex":["male","male","fmale"]
}
df=pd.DataFrame(dicts)
print(df)
'''
age name sex
0 20 lemon male
1 15 jack male
2 30 jason fmale
'''
#DataFrame可拆分成多个Series
print(df['name'])
print(type(df['name']))
'''
0 lemon
1 jack
2 jason
Name: name, dtype: object
<class 'pandas.core.series.Series'>
'''
#多个Series可组成DataFrame
name=df['name']
age=df['age']
print(name)
print(age)
#name和age两个Series组成DataFrame
print(pd.DataFrame([name,age]))
'''
0 1 2
name lemon jack jason
age 20 15 30
这么看结果和原DataFrame不一致,可以利用 .T转换
'''
print(pd.DataFrame([name,age]).T)
'''
name age
0 lemon 20
1 jack 15
2 jason 30
'''
#逐行读取DataFrame数据
for index,value in df.iterrows():
print(index)
print('---------')
print(value)
'''
0
---------
age 20
name lemon
sex male
Name: 0, dtype: object
1
---------
age 15
name jack
sex male
Name: 1, dtype: object
2
---------
age 30
name jason
sex fmale
Name: 2, dtype: object
'''
#仅读取sex和age的值
for index,value in df.iterrows():
age,name,sex=value
print(age)
print(sex)
'''
20
male
15
male
30
fmale
'''
网友评论