Pandas-数据结构-DataFrame(四):行索引【df[1:3];单标签:loc/iloc[-1];多标签:loc/iloc[[3, 2]];多行索引:loc/iloc[1:3]】

只选择一行输出Series,选择多行输出Dataframe

按照行切片索引: df[row_index_start : row_index_end]

  • df[]中为数字时,默认选择行,且只能进行切片的选择,不能单独选择( df[0]是错误的)
  • 输出结果为Dataframe,即便只选择一行
  • df[]不能通过索引标签名来选择行(df['one'])
  • df[]:利用 默认位置下标 来获取想要的行【末端不包含】
import numpy as np
import pandas as pd

df1 = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                   index=['one', 'two', 'four', 'three'],
                   columns=['a', 'b', 'c', 'd'])

print("df1 = \n{0}\ntype(df1) = {1}".format(df1, type(df1)))
print('-' * 50)

df2 = df1[1:3]

print("多行索引:df2 = df1[1:3] = \n{0}\ntype(df2) = {1}".format(df2, type(df2)))
print('-' * 100)

打印结果:

df1 =
               a          b          c          d
one    14.697748  84.130102  75.636127  65.541925
two    25.242130  53.488123  45.072336  24.906057
four   94.686317  88.176227  67.092432  35.897882
three  36.527603  70.150568  27.110961  45.964728
type(df1) = <class 'pandas.core.frame.DataFrame'>

Process finished with exit code 0

1、单标签索引:df.loc[1]、df.loc[‘one’]

  • 按照行名索引:df.loc[row_name]
  • 按照行下标索引:df.loc[row_index]
import numpy as np
import pandas as pd

df1 = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                   index=['one', 'two', 'three', 'four'],
                   columns=['a', 'b', 'c', 'd'])

df2 = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                   columns=['a', 'b', 'c', 'd'])

print("df1 = \n{0}\ntype(df1) = {1}".format(df1, type(df1)))
print('-' * 50)
print("df2 = \n{0}\ntype(df2) = {1}".format(df2, type(df2)))
print('-' * 100)

data1 = df1.loc['one']
data2 = df2.loc[1]
print("单标签索引:data1 = \ndf1.loc['one'] = \n{0}\ntype(data1) = {1}".format(data1, type(data1)))
print('-' * 50)
print("单标签索引:data2 = \ndf2.loc[1] = \n{0}\ntype(data2) = {1}".format(data2, type(data2)))
print('-' * 100)

打印结果:

df1 =
               a          b          c          d
one    93.037642  52.895322  42.547540  95.435676
two    24.088954  56.966169  79.185705  48.582922
three  76.162602  32.962263  41.853371  99.138612
four   24.979909  10.191909  27.335317  20.452524
type(df1) = <class 'pandas.core.frame.DataFrame'>
单标签索引:data1 =
df1.loc['one'] =
a    93.037642
b    52.895322
c    42.547540
d    95.435676
Name: one, dtype: float64
type(data1) = <class 'pandas.core.series.Series'>
df2 =
           a          b          c          d
0  21.656858  31.404614  88.520987  41.839721
1  26.884644   9.943081  91.739139  81.479288
2  96.522109  71.673956  55.843560  38.131336
3  73.574839  93.350715  89.358183  45.521198
type(df2) = <class 'pandas.core.frame.DataFrame'>
多标签索引:data4 =
df2.loc[[3, 2, 1]] =
           a          b          c          d
3  73.574839  93.350715  89.358183  45.521198
2  96.522109  71.673956  55.843560  38.131336
1  26.884644   9.943081  91.739139  81.479288
type(data4) = <class 'pandas.core.frame.DataFrame'>

3、多行索引:df.loc[1:3]、df.loc[‘one’ : ‘three’]

df.loc[]

  • df.loc[]:利用 index的名称 来获取想要的行【末端包含】
  • 核心:df.loc[label]主要针对index选择行,同时支持指定index,及默认数字index
  • 其中的int类型的索引时索引的名称,而非下标位置信息;
import numpy as np
import pandas as pd

df1 = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                   index=['one', 'two', 'four', 'three'],
                   columns=['a', 'b', 'c', 'd'])

print("df1 = \n{0}\ntype(df1) = {1}".format(df1, type(df1)))
print('-' * 50)

data3 = df1.loc['one':'three']
print("多行索引:data3 = \ndf1.loc['one':'three'] = \n{0}\ntype(data3) = {1}".format(data3, type(data3)))
print('-' * 100)

df2 = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                   index=[2, 3, 1, 0],
                   columns=['a', 'b', 'c', 'd'])
print("df2 = \n{0}\ntype(df2) = {1}".format(df2, type(df2)))
print('-' * 50)
data4 = df2.loc[1:0]
print("多行索引:data4 = \ndf2.loc[1:0] = \n{0}\ntype(data4) = {1}".format(data4, type(data4)))
print('-' * 100)

打印结果:

df1 =
               a          b          c          d
one    23.078737   9.156431   0.439799  64.906356
two    91.265745  12.581287  96.020470  95.584070
four   95.501825  57.346484  73.247475  58.338678
three  29.789496  95.718276  30.426301  94.037233
type(df1) = <class 'pandas.core.frame.DataFrame'>
df2 =
           a          b          c          d
2  56.269699  50.504353  25.417596  65.251456
3  93.865887  64.941123  92.814477  36.161766
1  45.549343  58.922568  84.581590  75.418238
0  38.015140  24.555141  41.455598  92.127194
type(df2) = <class 'pandas.core.frame.DataFrame'>

Process finished with exit code 0
  • df.iloc[] – 按照整数位置(从轴的0到length-1)选择行
  • 类似list的索引,其顺序就是dataframe的整数位置,从0开始计
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                   index=['two', 'three', 'one', 'four'],
                   columns=['a', 'b', 'c', 'd'])

print("df = \n{0}\ntype(df) = {1}".format(df, type(df)))
print('-' * 100)

data1 = df.iloc[0]
data2 = df.iloc[-1]
print("data1 = df.iloc[0] = \n{0}\ntype(data1) = {1}".format(data1, type(data1)))
print('-' * 50)
print("data2 = df.iloc[-1] = \n{0}\ntype(data2) = {1}".format(data2, type(data2)))

打印结果:

df =
               a          b          c          d
two    24.860960  54.227202  18.018653  38.724716
three  69.652166  97.651980  19.959022  24.155129
one    31.995642  59.591356  76.431234  44.830302
four   33.716382  32.102688  20.937836  73.288219
type(df) = <class 'pandas.core.frame.DataFrame'>
data2 = df.iloc[-1] =
a    33.716382
b    32.102688
c    20.937836
d    73.288219
Name: four, dtype: float64
type(data2) = <class 'pandas.core.series.Series'>

Process finished with exit code 0
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                  index=['two', 'three', 'one', 'four'],
                  columns=['a', 'b', 'c', 'd'])

print("df = \n{0}\ntype(df) = {1}".format(df, type(df)))
print('-' * 100)

data1 = df.iloc[[0, 2]]
data2 = df.iloc[[3, 2, 1]]
print("data1 = df.iloc[[0, 2]] = \n{0}\ntype(data1) = {1}".format(data1, type(data1)))
print('-' * 50)
print("data2 = df.iloc[[3, 2, 1]] = \n{0}\ntype(data2) = {1}".format(data2, type(data2)))

打印结果:

df =
               a          b          c          d
two     4.708440  22.863487  26.806435   3.948613
three  43.713656  75.754603  54.269785  64.708510
one    49.566989  89.956527  26.388450  54.651651
four   31.750995  81.558108  45.912672  40.851126
type(df) = <class 'pandas.core.frame.DataFrame'>
data2 = df.iloc[[3, 2, 1]] =
               a          b          c          d
four   31.750995  81.558108  45.912672  40.851126
one    49.566989  89.956527  26.388450  54.651651
three  43.713656  75.754603  54.269785  64.708510
type(data2) = <class 'pandas.core.frame.DataFrame'>

Process finished with exit code 0
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(16).reshape(4, 4) * 100,
                  index=['two', 'three', 'one', 'four'],
                  columns=['a', 'b', 'c', 'd'])

print("df = \n{0}\ntype(df) = {1}".format(df, type(df)))
print('-' * 100)

data1 = df.iloc[0:2]
data2 = df.iloc[1:3]
print("data1 = df.iloc[0:2] = \n{0}\ntype(data1) = {1}".format(data1, type(data1)))
print('-' * 50)
print("data2 = df.iloc[1:3] = \n{0}\ntype(data2) = {1}".format(data2, type(data2)))

打印结果:

df =
               a          b          c          d
two    58.579340  15.748046  11.903897  90.569674
three  64.781174  49.745905   5.778577  99.143819
one    96.295298  61.041770  61.024144   9.930110
four   36.892635  26.641423  16.890470  43.553212
type(df) = <class 'pandas.core.frame.DataFrame'>
data2 = df.iloc[1:3] =
               a          b          c          d
three  64.781174  49.745905   5.778577  99.143819
one    96.295298  61.041770  61.024144   9.930110
type(data2) = <class 'pandas.core.frame.DataFrame'>

Process finished with exit code 0

前提,简单介绍一下它俩:

  • loc利用 index的名称,来获取想要的行(或列)【名称导向】
  • iloc利用 index的具体位置(所以它只能是整数型参数),来获取想要的行(或列)。
import numpy as np
import pandas as pd

s = pd.Series(np.nan, index=[49, 48, 47, 46, 45, 1, 2, 3, 4, 5])
print("s = \n", s)

打印结果:

s =
49   NaN
48   NaN
47   NaN
46   NaN
45   NaN
1    NaN
2    NaN
3    NaN
4    NaN
5    NaN
dtype: float64

让我们用整数3来试着提数

  • s.iloc[:3]返回给我们的是前3行的数(因为把3当作位置信息做的处理);
  • s.loc[:3]返回前8行得数(因为把3当作名称对象做的处理);
import numpy as np
import pandas as pd

s = pd.Series(np.nan, index=[49, 48, 47, 46, 45, 1, 2, 3, 4, 5])
print("s.iloc[:3] = \n", s.iloc[:3])
print("-" * 50)
print("s.loc[:3] = \n", s.loc[:3])

打印结果:

`python
s.iloc[:3] =
49 NaN
48 NaN
47 NaN
dtype: float64

Original: https://blog.csdn.net/u013250861/article/details/124065734
Author: u013250861
Title: Pandas-数据结构-DataFrame(四):行索引【df[1:3];单标签:loc/iloc[-1];多标签:loc/iloc[[3, 2]];多行索引:loc/iloc[1:3]】

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/744007/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球