pandas iloc函數


 

import pandas as pd import numpy as np import matplotlib.pyplot as plt # 設置輸出結果列對齊 pd.set_option('display.unicode.ambiguous_as_wide',True) pd.set_option('display.unicode.east_asian_width',True) 
In [100]:
mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 100, 'b': 200, 'c': 300, 'd': 400}, {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }, {'a': 10000, 'b': 20000, 'c': 30000, 'd': 40000 }, {'a': 100000, 'b': 200000, 'c': 300000, 'd': 400000 }] df = pd.DataFrame(mydict) # 插入一個列-學號 # student_ID = pd.Series([101,102,103,104,105],name = 'ID') # df.insert(0,column='ID',value = student_ID) # df.set_index(['ID'],inplace = True) # print(df.columns) df 
Out[100]:
  a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
3 10000 20000 30000 40000
4 100000 200000 300000 400000
In [101]:
print(type(df.iloc[0])) df.iloc[0] 
 
<class 'pandas.core.series.Series'>
Out[101]:
a    1
b    2
c    3
d    4
Name: 0, dtype: int64
In [102]:
print(type(df.iloc[[0]])) df.iloc[[0]] 
 
<class 'pandas.core.frame.DataFrame'>
Out[102]:
  a b c d
0 1 2 3 4
In [103]:
print(type(df.iloc[1])) df.iloc[1] 
 
<class 'pandas.core.series.Series'>
Out[103]:
a    100
b    200
c    300
d    400
Name: 1, dtype: int64
In [104]:
print(type(df.iloc[[1]])) df.iloc[[1]] 
 
<class 'pandas.core.frame.DataFrame'>
Out[104]:
  a b c d
1 100 200 300 400
In [105]:
print(type(df.iloc[[1,2,3]])) df.iloc[[1,2,3]] 
 
<class 'pandas.core.frame.DataFrame'>
Out[105]:
  a b c d
1 100 200 300 400
2 1000 2000 3000 4000
3 10000 20000 30000 40000
In [106]:
print(type(df.iloc[1:3])) df.iloc[1:4] 
 
<class 'pandas.core.frame.DataFrame'>
Out[106]:
  a b c d
1 100 200 300 400
2 1000 2000 3000 4000
3 10000 20000 30000 40000
In [ ]:
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM