官网是这样解释的:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.htmlas_index:bool, default TrueFor aggregated output, return object with group labels as the index. Only ...
函数pandas.DataFrame.groupby参数as_index的意义
含义:as_index决定了分组使用的属性是否成为新的表格的索引,默认是as_index=True,我的代码中常用:as_index=False.
使用作为索引只是会影响查询速度,而一般没有这样的需求。
as_index=True是常用的表格形式,而as_index=False除了表格有变化,显示也会不同。
文档 ...
#as_index=False结果的列名与之前一致
aa=chipo.groupby(['item_name'],as_index=False)['quantity'].sum()
#大类的销售金额 reset_index(drop=True) 删除原index
daleijine=df.groupby(['大类名称'],as_index=False)['销售金额'].sum().sort_values(['销售金额'],ascending=False).reset_index(drop=True)
df = pd.DataFrame(data = {'book':['bk1','bk1','bk2','bk2','bk3'],
'price':['12','12','5','5','45']})
print(df)
print(df.groupby('book',as_index = True).sum())
print(df.groupby('book',as_index = False).sum())
output:
books
#encoding设定编码
#header = None 把第一列让出
data=pd.read_csv(‘data (2).csv’,encoding=‘GBK’,header=None)
#给列名重命名
data.columns=[‘a’,‘b’,‘c’,‘d’]
#打印前五行 filename.head(5)
data=data
我们经常需要对某些标签或索引的局部进行累计分析,这时候需要用到groupby函数了。
其中groupby函数的as_index参数有以下介绍:
as_index : boolean, default True
For aggregated output, return object with group labels as the index. Only relevant for DataFram...
groupby官方解释
DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, **kwargs)
Group series using mapper (dict or key function, apply given function to group, return result as series) or by a series of columns.
讲真的,非常不能理解pandas官方文档的这种表达形式,让人真的有点摸不着
Easiest way to install:
Load the program group AviDemo.bpg, install the package AviPack.dpk(bpl),
try the demos.
Read the source of AviWriter_2.pas (in AviPack) to get help on what the
procedures and properties do.
**Current version:
AviWriter_2 ver 1.0.0.4
Changes:
Finally got On-the-fly compression working with still
being able to add an audio-stream.
Use property OnTheFlyCompression (default is true)
Also, now more than one audio file can be added.
For each wav-file a delay (ms) can be specified,
which says when it'll start playing.
Use method AddWaveFile.
In 1.0.0.3 the delay got too short. Now
it seems to work, due to really adding "silence"
from the end of the previous audio file.
Note:
Some Codecs don't support On-the-fly compression.
If OnTheFlyCompression is false, only one wave file
can be added.
**A list of codec-gotchas:
(still unclear about the exact range of occurrance)
IV50 Indeo Video 5:
Both frame dimensions must be a factor of 4.
DIVX DivX codec:
Both frame dimensions must be a factor of 4.
Gives a floating point error under certain circumstances.
More and more likely that this occurs if the frames are
too "different".
If this happens, then there's an AV in Avifil32.dll,
don't know how to prevent this.
The codec compresses real movies nicely at frametimes <=60 ms,
when changing the settings in its dialog box as follows:
Bitrate (1st page): to >=1300
Max Keyframe interval (2nd page): to <=20.
MRLE MS-RLE
Use that one if you want to make avis which play
transparently in a TAnimate.
(Thanks Eddie Shipman)
But it does not support on-the-fly compression.
Whenever a codec fails to work, there's this dreaded error
"A call to an operating system function failed" while writing
a compressed file, or an unhandled exception.
The only way to prevent it, that I see, is, to
collect more peculiarities about the codecs and stop execution
in case of certain combinations of settings. When queried about
their capabilities, some of these guys seem to lie.
Renate Schaaf
[email protected]
Pandas数据分析groupby函数深度总结(1)groupby分组数据加载数据数据分组按'Sales Rep'列分组显示所有分组选择一个特定的组计算每组中的行数按'Sales Rep'中的姓分组按'Sales Rep'中是否包含有“William”分组按随机序列分组按'Val'列分位数分成三组按制定的'Val'列的范围进行分组pd.GrouperGrouping by year按季度或其他频率分组通过多列进行分组
pandas包最强大的函数之一,当属groupby了。但是大多数人对groupby函数研究
DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, observed=False, **kwargs)
其中,最常用的参数为by,它可以指定按照哪些列进行分组。例如:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob', 'Charlie'],
'Subject': ['Math', 'Math', 'Math', 'Science', 'Science', 'Science'],
'Score': [80, 90, 85, 95, 92, 89]}
df = pd.DataFrame(data)
# 按照Name列进行分组,并计算每个分组的平均值
result = df.groupby('Name').mean()
print(result)
输出结果为:
Score
Alice 87.5
Bob 91.0
Charlie 87.0
上述代码中,我们按照Name列进行分组,并对每个分组的Score列求均值。最终得到了每个人的平均成绩。需要注意的是,groupby函数返回的是一个GroupBy对象,我们可以对其进行各种聚合操作,例如mean、sum、count等。