I have a pandas column containing some numbers for example:
A
1.10
1.11
1.00
2.10
2.11
2.10
3.10
3.11
3.12
I want to group the numbers in groups of 1's 2's and 3's how can I do that? so basically I want to specify the spread so if the spread is for example 1.00+-0.5 then I group all numbers falling within that range and then take the average of them by putting them in an array. I have tried the groupby pd.cut() but did not achieve the expected results. Please help!
Your data.
d = {"A" : [1.10, 1.11, 1.00, 2.10, 2.11, 2.10, 3.10, 3.11, 3.12]}
df = pd.DataFrame.from_dict(d)
To group your data into 1, 2 and 3 - you can simply take the first int of the float and convert it into a string in a new column (here: grouping_of_A).
df["grouping_of_A"] = df["A"].map(lambda x: str(x)[0])
print(df)
A grouping_of_A
0 1.10 1
1 1.11 1
2 1.00 1
3 2.10 2
4 2.11 2
5 2.10 2
6 3.10 3
7 3.11 3
8 3.12 3
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 9 entries, 0 to 8
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 A 9 non-null float64
1 grouping_of_A 9 non-null object
dtypes: float64(1), object(1)
memory usage: 272.0+ bytes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.