【发布时间】:2019-12-30 03:13:01
【问题描述】:
Numpy:
import numpy as np
nparr = np.array([[1, 5],[2,6], [3, 7]])
print(nparr)
print(nparr[0]) #first choose the row
print(nparr[0][1]) #second choose the column
按预期给出输出:
[[1 5]
[2 6]
[3 7]]
[1 5]
5
熊猫:
df = pd.DataFrame({
'a': [1, 2, 3],
'b': [5, 6, 7]
})
print(df)
print(df['a']) #first choose the column !!!
print(df['a'][1]) #second choose the row !!!
给出以下输出:
a b
0 1 5
1 2 6
2 3 7
0 1
1 2
2 3
Name: a, dtype: int64
2
将 Pandas 数据框中“索引”的默认排序更改为列优先的根本原因是什么?失去一致性/直观性会给我们带来什么好处?
当然,如果我使用 iloc 函数,我们可以将其编码为类似于 Numpy 数组索引:
print(df)
print(df.iloc[0]) # first choose the row
print(df.iloc[0][1]) # second choose the column
a b
0 1 5
1 2 6
2 3 7
a 1
b 5
Name: 0, dtype: int64
5
【问题讨论】:
-
我认为 DataFrame 由 Series 组成。系列/列可以在
dtype中有所不同。numpy具有结构化数组,其字段具有自己的数据类型。
标签: python pandas numpy dataframe