首先创建一个DataFrame
>>> import pandas as pd
>>> df= pd.DataFrame({
'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Yum Yum'],
'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
'rating': [4, 4, 3.5, 15, 5]
})
>>> df
brand style rating
0 Yum Yum cup 4.0
1 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Yum Yum pack 5.0
来到赋值场景:
- 需求:需要对 rating 列的[0, 1, 4]行的3个元素赋值为[[5], [5], [4]], 也就是同时使用位置索引(positional indexing)和标签索引(label-based indexing)
- 官方建议的操作为:
>>> import numpy as np
>>> df.loc[df.index[[0, 1, 4]], 'rating'] = np.array([5, 5, 4]).reshape((3, 1))
>>> df
brand style rating
0 Yum Yum cup 5.0
1 Yum Yum cup 5.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Yum Yum pack 4.0
>>> #或者
>>> df.iloc[[0, 1, 4], df.columns.get_loc('rating')] = np.array([5, 5, 4]).reshape((3, 1))
>>> df
brand style rating
0 Yum Yum cup 5.0
1 Yum Yum cup 5.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Yum Yum pack 4.0
>>>#或者
>>> df.iloc[df.index[[0, 1, 4]], df.columns.get_loc('rating')] = np.array([5, 5, 4]).reshape((3, 1))
>>> df
brand style rating
0 Yum Yum cup 5.0
1 Yum Yum cup 5.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Yum Yum pack 4.0
>>>#或者,使用.get_indexer()可以同时索引多列如.get_indexer(['style', 'rating'])
>>> df.iloc[df.index[[0, 1, 4]], df.columns.get_indexer(['rating'])] = np.array([4, 4, 5]).reshape((3, 1))
>>> df
brand style rating
0 Yum Yum cup 4.0
1 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Yum Yum pack 5.0