set_index_from_0_to_n
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
set_index_from_0_to_n [2022/03/09 22:24] – created raju | set_index_from_0_to_n [2022/03/16 21:55] (current) – [Use reset_index()] raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== set index from 0 to N ==== | + | ===== Task ===== |
+ | The idea here is to reset index such that they are all integers | ||
- | Task:- The idea here is to reset index such that they are all integers from 0 to N | + | ===== Solution ===== |
+ | Two possible approaches: | ||
- | https:// | + | ==== Use reset_index() ==== |
- | tags | range, uses np.arange, reindex from 0 to N | + | To not modify the original df but assign the result |
+ | < | ||
+ | df_new = df.reset_index(drop=True) | ||
+ | </ | ||
- | See also | https:// | + | To do it in place |
+ | < | ||
+ | df.reset_index(drop=True, | ||
+ | </ | ||
+ | |||
+ | Sample code: https:// | ||
+ | |||
+ | ==== use reindex to directly change the index ==== | ||
+ | |||
+ | < | ||
+ | df.index = np.arange(len(df)) | ||
+ | </ | ||
+ | |||
+ | Sample code: https:// | ||
+ | |||
+ | tags | reindex from 0 to N, range, uses np.arange | ||
+ | |||
+ | |||
+ | ==== tags ==== | ||
+ | set index from 0 to N | ||
+ | |||
+ | ==== See also ==== | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
set_index_from_0_to_n.1646864681.txt.gz · Last modified: 2022/03/09 22:24 by raju