python中Pandas之DataFrame索引、选取数据

总结一下

索引问题

1.1 认识索引

先创建一个简单的 DataFrame

myList = [['a', 10, 1.1],
      ['b', 20, 2.2],
      ['c', 30, 3.3],
      ['d', 40, 4.4]]
df1 = pd.DataFrame(data = myList)
print(df1)

print(df1.columns)
[out]:
RangeIndex(start=0, stop=3, step=1)
[out]:
      char  int  float
one      a   10    1.1
two      b   20    2.2
three    c   30    3.3
four     d   40    4.4

输出此时的行索引和列索引:


print(df2.index)
[out]:
Index(['one', 'two', 'three', 'four'], dtype='object')
type(df2[['char']])
[out]:pandas.core.frame.DataFrame

注意直接使用 df2[0] 取某一列会报错,除非 columns是由下标索引组成的,比如 df1那个样子, df1[0] 就不会报错。

`python
print(df1[0])
[out]:
0 a
1 b
2 c
3 d
Name: 0, dtype: object
print(df2.loc[[‘one’, ‘three’]])
[out]:
char int float
one a 10 1.1
three c 30 3.3
print(df2.iloc[[0, 2]])
[out]:
char int float
one a 10 1.1
three c 30 3.3

Original: https://blog.csdn.net/weixin_46713695/article/details/125959391
Author: 赵孝正
Title: python中Pandas之DataFrame索引、选取数据

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

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

(0)

大家都在看

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