08-pandas 深入理解 Series/DataFrame

 

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

 

data = {
"Country": ['India','England', 'Franch'],
"Captial": ['Mengmai', 'London', 'Bolin'],
"Population": ["1500", '20', '25']
}

 

 

Series


 

根据数据创建 Series

 

s1 = np.Series(data['Country'])

0      India
1    England
2     Franch
dtype: object

s1.values

array(['India', 'England', 'Franch'], dtype=object)

s1.index

RangeIndex(start=0, stop=3, step=1)




指定index的值

s2 = np.Series(data['Country'], index= ['a', 'b', 'c'])

a      India
b    England
c     Franch
dtype: object

s2.values

array(['India', 'England', 'Franch'], dtype=object)

s2.index

Index(['a', 'b', 'c'], dtype='object')

 

 

 

 

 

 

DataFrame


 

 

1. 创建 DataFrame 

df1 = pd.DataFrame(data)

 

country = df1['Country']

 

 typeof(country)

pandas.core.series.Series

 

df1.iterrows()

<generator object DataFrame.iterrows at 0x00000187A814B048>

 

df1.iterrows

<bound method DataFrame.iterrows of    Country  Captial Population
0    India  Mengmai       1500
1  England   London         20
2   Franch    Bolin         25>

 

 

 

 2.  df1.iterrows()  生成器 , 可以进行 for..in.. 循环, 每一行产生一个 tuple 

for item in df1.iterrows():
  print(item)

 

(0, Country         India
Captial       Mengmai
Population       1500
Name: 0, dtype: object)
(1, Country       England
Captial        London
Population         20
Name: 1, dtype: object)
(2, Country       Franch
Captial        Bolin
Population        25
Name: 2, dtype: object)

 

 3. 剖析 DataFrame 的组成

for item in df1.iterrows():
print(item)
print("____________________________")
print(len(item))
print("____________________________")
print(item[0]," \n- - - - - - -\n ",item[1])
print("____________________________")
print(type(item),type(item[0]), type(item[1]))
print("\n\n\n\n")

 

 

 

(0, Country         India
Captial       Mengmai
Population       1500
Name: 0, dtype: object)
____________________________
2
____________________________
0  
- - - - - - -
  Country         India
Captial       Mengmai
Population       1500
Name: 0, dtype: object
____________________________
<class 'tuple'> <class 'int'> <class 'pandas.core.series.Series'>





(1, Country       England
Captial        London
Population         20
Name: 1, dtype: object)
____________________________
2
____________________________
1  
- - - - - - -
  Country       England
Captial        London
Population         20
Name: 1, dtype: object
____________________________
<class 'tuple'> <class 'int'> <class 'pandas.core.series.Series'>





(2, Country       Franch
Captial        Bolin
Population        25
Name: 2, dtype: object)
____________________________
2
____________________________
2  
- - - - - - -
  Country       Franch
Captial        Bolin
Population        25
Name: 2, dtype: object
____________________________
<class 'tuple'> <class 'int'> <class 'pandas.core.series.Series'>

 

 

4. 构建 DataFrame 逆转


s_1 = pd.Series( data["Capital"])

s_2 = pd.Series( data["Country"])

s_3 = pd.Series( data["Population"])

 

df_new = pd.DataFrame([s_1, s_2, s_3]) 

 

df_new

 

df1

 

df_new.T

df_new.T,使用大 T 进行逆转

 

 

5. 使用 索引进行构建有表头的  DataFrame

df_new2 = pd.DataFrame([s_1, s_2, s_3],  index = ['Capital', 'Country', 'Population']).T

 

 

 

 

6. Series 和 DataFrame

 

posted @ 2019-06-12 20:39  aocn  阅读(342)  评论(0)    收藏  举报