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]:
 abcd
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]:
 abcd
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]:
 abcd
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]:
 abcd
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]:
 abcd
1 100 200 300 400
2 1000 2000 3000 4000
3 10000 20000 30000 40000
In [ ]:
 
原文地址:https://www.cnblogs.com/vincent-sh/p/12827577.html