I have huge dataset where i need to subset the data based on the letter it begins with.
df:
name | price |
---|---|
Apple | 20 |
Orange | 35 |
Watermelon | 10 |
Banana | 5 |
I need to subset data that beginswith a to i in as one set, j to r as another, s to z as another set. Is there any other way than below
alpha1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’]
alpha2 = [‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’]
alpha3 = [‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]
df1 = df[df['name'].str.startswith(alpha1)]
df2 = df[df['name'].str.startswith(alpha2)]
df3 = df[df['name'].str.startswith(alpha3)]
df1 = df.loc[df['name'][0] in 'abcdefghi']
df2 = df.loc[df['name'][0] in 'jklmnopqr']
df3 = df.loc[df['name'][0] in 'stuvwxyz']
Try this, hope this works
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.