Article Directory
1.pandas.DataFrame.query
DataFrame.query(expr, inplace=False, **kwargs)
description
Use Boolean expressions to query the columns of the DataFrame
parameter
expr:
the query string to be calculated by str
inplace: bool
query should modify the data or return the modified copy
** kwargs
return
DataFrame or None DataFrame
generated by the provided query expression; if inplace = True, then None
Official case
df = pd.DataFrame({
'A': range(1, 6),
'B': range(10, 0, -2),
'C C': range(10, 5, -1)})
df
A B C C
0 1 10 10
1 2 8 9
2 3 6 8
3 4 4 7
4 5 2 6
df.query('A > B')
A B C C
4 5 2 6
# 前面的表达式等价于
df[df.A > df.B]
A B C C
4 5 2 6
# 对于名称中有空格的列,可以使用反引号
df.query('B == `C C`')
A B C C
0 1 10 10
# 前面的表达式等价于
df.query('B == `C C`')
A B C C
0 1 10 10
2.pandas.DataFrame.reset_index
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
description
重置索引或索引的级别。
重置DataFrame的索引,并使用默认索引。
如果DataFrame具有MultiIndex,则此方法可以删除一个或多个级别
parameter
level: int, str, tuple, or list, default None
only removes the given level from the index, all levels are removed by default.
drop: bool, default False
Don't try to insert the index into dataframe columns, this will reset the index to the default integer index
inplace: bool, default False
if True, then modify the original object
if False, then return the modified object
col_level: int or str, default 0
If the column has multiple levels, please determine which level to insert the label into. By default, it is inserted into the first level.
col_fill: object, default''If the
column has multiple levels, determine how to name the other levels. If it is None, the index name is repeated.
return
DataFrame or None
if False, the DataFrame with the new index; if inplace = True, it is None
Official case
When resetting the index, the old index will be added as a column and the new sequential index
will be used. We can use the drop parameter to avoid adding the old index as a column
Use reset_index with MultiIndex
If we do not delete the index, it is at the top level by default.
We can put it at another level:
when the index is inserted under another level, we can use the parameter col_fill to specify which level
If we specify a non-existent level for col_fill, it will be created