我想按for (i = 0; i < countryList.length; i++)
{
if (destination.toLowerCase().indexOf(countryList[i].toLowerCase()) !== -1)
{
break;
}
});
if (i < countryList.length)
{
// i is the index of the country found
}
,col1
和col2
分组,并根据col4
找到每个分组的最大值和最小值
col3
产生此:
import pandas as pd
d = {'col1': [1,1,2,3,3,4,4,4,5,5,5,6,6,6,7,7],
'col2': ['w', 'w','w','w','w','e','e','e','e','e','e','t','t','t','t','t'],
'col3': [ 1,4,1,1,2,3,5,8,2,3,3,5,3,3,4,3], #in every combination this decides the max or min
'col4': [ 5,5,6,6,6,3,3,3,1,1,1,4,4,4,6,6]}
df = pd.DataFrame(data=d)
df
我尝试过:
col1 col2 col3 col4
0 1 w 1 5 #1w5 is one group where by col3 has max 4 and min 1
1 1 w 4 5 #thus we make a col `max` with 4 and one `min` with 1
2 2 w 1 6
3 3 w 1 6
4 3 w 2 6
5 4 e 3 3
6 4 e 5 3
7 4 e 8 3
8 5 e 2 1
9 5 e 3 1
10 5 e 3 1
11 6 t 5 4
12 6 t 3 4
13 6 t 3 4
14 7 t 4 6
15 7 t 3 6
返回此:
ValueError:对象类型没有名为col2的轴
答案 0 :(得分:0)
您必须传递要对其进行分组依据的列的列表,并为新列定义名称
df.groupby(['col1','col2','col4'])['col3'].max().reset_index(name ='Max')
输出
col1 col2 col4 Max
1 w 5 4
2 w 6 1
3 w 6 2
4 e 3 8
5 e 1 3
6 t 4 5
7 t 6 4
或者要获取完整列表,请使用transform
df['Max']=df.groupby(['col1','col2','col4'])['col3'].transform('max')
输出:
col1 col2 col3 col4 Max
1 w 1 5 4
1 w 4 5 4
2 w 1 6 1
3 w 1 6 2
3 w 2 6 2
4 e 3 3 8
4 e 5 3 8
4 e 8 3 8
5 e 2 1 3
5 e 3 1 3
5 e 3 1 3
6 t 5 4 5
6 t 3 4 5
6 t 3 4 5
7 t 4 6 4
7 t 3 6 4