Peng He
Peng He

Reputation: 2213

pandas DataFrame: How to cut a dataframe using custom ways?

I want to cut a DataFrame to several dataframes using my own rules.

>>> data = pd.DataFrame({'distance':[1,2,3,4,5,6,7,8,9,10],'values':np.arange(0,1,0.1)})
>>> data
   distance  values
0         1     0.0
1         2     0.1
2         3     0.2
3         4     0.3
4         5     0.4
5         6     0.5
6         7     0.6
7         8     0.7
8         9     0.8
9        10     0.9

I'll cut data according to values of distance column. For example, there's some bins [1,3),[3,8),[8,10),[10,10+), if data's column distance in same bin,I separate them into same group and compute column values average value or sum value.That is

>>> data1 = data[lambda df:(df.distance >= 1) & (df.distance < 3)]
>>> data1
   distance  values
0         1     0.0
1         2     0.1
>>> np.mean(data1['values'])
0.05

How can I cut origin DataFrame into several groups(and then save them,process them...) efficiently?

Upvotes: 3

Views: 8347

Answers (1)

Bob Baxley
Bob Baxley

Reputation: 3751

Pandas cut command is useful for this:

data['categories']=pd.cut(data['distance'],[-np.inf,1,3,8,10,np.inf],right=False)
data.groupby('categories').mean()

Output:

            distance    values
categories      
[-inf, 1)   NaN     NaN
[1, 3)      1.5     0.05
[3, 8)      5.0     0.40
[8, 10)     8.5     0.75
[10, inf)   10.0    0.90

Upvotes: 4

Related Questions