I have a dataset of this type:
| 0 | 1 | 2 |
|---|---|---|
| 0:0 | 57:0 | 166:0 |
| 0:5 | 57:20 | 166:27 |
| 0:10 | 57:8: | 166:36 |
| 0:27 | 57:4 | 166:45 |
i want to convert this dataframe into an ascending list. I want to check the whole data into ascending order and then create a list using the ascending order numbers in the dataframe. and ascending order should be of numbers before ‘:’
desired output:
| list |
|---|
| 0 |
| 57 |
| 166 |
>Solution :
You can unstack (or stack) to flatten to Series, then extract the number, convert to integer and keep the unique values in order:
For a python list you can try:
sorted(df.unstack()
.str.extract('(\d+):', expand=False)
.astype(int)
.unique().tolist())
output: [0, 57, 166]
As Series:
out = (df.unstack()
.str.extract('(\d+):', expand=False)
.astype(int)
.drop_duplicates()
.sort_values().reset_index(drop=True)
)
output:
0 0
1 57
2 166
dtype: int64