创建
可以利用
来创建DataFrame对象
如:
a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
a['new'] = ['鹅', '鹅', '鹅']
a.loc[3]=[0, 0, 0, '鹅']
b = a.copy()
df.drop()
添加 inplace=True 则在原数据修改,默认为False
①
b.drop(columns='new')
②
c = b.pop('new')
b
c
赋值并在原数据中删除
③
del b['new']
b.drop(index=3)
这里改查放在一起,因为查到即可更改
a = np.random.normal(0, 1, (3, 3))
a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
a['abc'] = ['a', 'b', 'c']
Access a group of rows and columns by label(s) or a boolean array.
a = np.random.normal(0, 1, (3, 3))
a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
a['abc'] = ['a', 'b', 'c']
a.loc[0, 0]
0.41059850193837233
a.loc[0, 'abc']
‘a’
a.loc[a.abc != 'b', 'abc']
Purely integer-location based indexing for selection by position.
a.iloc[0, 3]
a.iloc[0]
a.iloc[[0, 1]]
a.iloc[0:3]
a.iloc[:, 3]
使用[ ]进行查找
a = np.random.normal(0, 1, (3, 3))
a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
a['abc'] = ['a', 'b', 'c']
查单列
a[1]
查多列
a[[1, 'abc']]
按单个条件查询
a[a[1] > 0.1]
a[a.index != 1]
按多个条件查询
# Satisfying multiple conditions simultaneously
a[(a[1] > 0.1) & (a[0] > 0)]
# At least one condition is met
a[(a[1] > 0.1) | (a[0] > 0)]
Reset the index of the DataFrame, and use the default one instead. If the DataFrame has a MultiIndex, this method can remove one or more levels.
a
a.index = ['c', 'd', 'e']
a.reset_index(drop=True, inplace=True)
Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don’t throw an error.
a.rename(index={0:9, 1:8}, inplace=True)
a.rename(columns={0:'cba', 1:'nba'}, inplace=True)