Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重命名pandas groupby对象的行索引_Python_Pandas - Fatal编程技术网

Python 重命名pandas groupby对象的行索引

Python 重命名pandas groupby对象的行索引,python,pandas,Python,Pandas,我有一个GroupBy对象,它的行索引是整数 light worst_injury count 1 5 10217 2 5 4067 3 5 2142 4 5 1690 5 5 25848 6 5 734 9 5 18 我想重新命名行(而不是列!),以便“

我有一个GroupBy对象,它的行索引是整数

light   worst_injury count
1       5            10217
2       5            4067
3       5            2142
4       5            1690
5       5            25848
6       5            734
9       5            18
我想重新命名行(而不是列!),以便“light”列包含特定字符串:

light    worst_injury    count
Day             5        10217
Dawn            5        4067
Dusk            5        2142
Dark- lit       5        1690
Dark- unlit     5        25848
Other           5        734
Unknown         5        18
我有一个字符串列表,对应于每个数字[‘Day’、‘Dawn’等],但我不知道如何在GroupBy函数调用之前或期间将它们设置为索引。我也尝试过制作透视表,但出于同样的原因,似乎也不可能这样做

我想我可以编写一个脚本,将原始数据更改为这些字符串,而不是数字。这似乎是一种效率较低的方法,但如果无法在事件发生后或之前更改groupby对象,我愿意接受这种选择

这是现有的代码;它按灯光和每个伤害级别对数据帧进行分组,然后进行计数:

df = pd.read_csv(filename, sep='|', usecols=['crash_deer_involv_assoc', 'worst_injury_in_accident', 'light', 'accident_month'])

for i in range(1,6):
    inj = df[(df['worst_injury_in_accident'] == i)] 
    grouped = inj.groupby(['light','worst_injury_in_accident'])
    grouped.agg('count')
IIUC可通过字典
d
使用:

#maybe first reset index
df = df.reset_index()

print df
   light  worst_injury  count
0      1             5  10217
1      2             5   4067
2      3             5   2142
3      4             5   1690
4      5             5  25848
5      6             5    734
6      9             5     18

d = {1:'Day',2:'Dawn', 3:'Dusk',4:'Dark- lit',5:'Dark- unlit',6:'Other',9:'Unknown'}

df['light'] = df.light.map(d)
print df
         light  worst_injury  count
0          Day             5  10217
1         Dawn             5   4067
2         Dusk             5   2142
3    Dark- lit             5   1690
4  Dark- unlit             5  25848
5        Other             5    734
6      Unknown             5     18
如果列
索引

print df
       worst_injury  count
light                     
1                 5  10217
2                 5   4067
3                 5   2142
4                 5   1690
5                 5  25848
6                 5    734
9                 5     18

d = {1:'Day',2:'Dawn', 3:'Dusk',4:'Dark- lit',5:'Dark- unlit',6:'Other',9:'Unknown'}

df.index = df.index.to_series().map(d)
print df
             worst_injury  count
light                           
Day                     5  10217
Dawn                    5   4067
Dusk                    5   2142
Dark- lit               5   1690
Dark- unlit             5  25848
Other                   5    734
Unknown                 5     18
编辑:

对于聚合,您可以将参数
添加为_index=False
,并且调用-输出为nice
DataFrame
,不需要:

print df.groupby(['light','worst_injury_in_accident'], as_index=False).count()
另一个选项是使用(对于我来说,
as_index
不起作用,所以需要调用)


顺便说一句:包含
NaN
值,但不包含。

您的第一个解决方案工作得非常好!reset_index()似乎非常有用。最初我得到一个属性错误“'DataFrame'对象没有属性'light'”,但当我将
df.light.map
更改为
df['light'].map时,它被修复了。谢谢你的帮助!非常感谢。我尝试添加更好的聚合解决方案,请参阅我的编辑。如果我的答案有用,请不要忘记。谢谢。这正是我想要的——一些我在搜索过程中错过的小选择或争论。谢谢你的详细回答!我接受了。
print df.groupby(['light','worst_injury_in_accident']).size().reset_index(name='count')