'Pandas returning 0 string as 0%
I'm doing a evaluation on how many stores report back in how many time (same day(0), 1 day(1), etc), but when calculate the percentage of the total, all same day stores return 0% of the total. I tried turning the column into object, float and int, but with the same result.
DF['T_days'] = (DF['day included in the server'] - DF['day of sale']).dt.days
create my T_Days and fills it with the amount in days based on the 2 datatime columns. This works fine. And by:
DF['Percentage'] = (DF['T_days'] /DF['T_days'].sum()) * 100
return this table. I know what i should do but now how to do it.
| COD_store | date in server | Date bought | T_days | Percentage |
|---|---|---|---|---|
| 1 | 2021-12-03 | 2021-12-02 | 1 | 0.013746 |
| 1 | 2021-12-03 | 2021-12-02 | 1 | 0.013746 |
| 922 | 2022-01-27 | 2022-01-10 | 17 | 0.233677 |
| 922 | 2022-01-27 | 2022-01-10 | 17 | 0.233677 |
| ... | ... | ... | ... | ... |
| 65 | 2022-01-12 | 2022-01-12 | 0 | 0.0 |
new DF after groupby:
T_DIAS
0 0.000000
1 1.374570
2 0.192440
3 15.793814
7 0.384880
17 82.254296
Name: Percentage, dtype: float64
I know i should divide the days resulted by the total amount of rows in DF and then group them by days, but my search on how to do this resulted in nothing. THW: i already have a separate DF for those days and percentage
Expected table:
| T_days | Percentage |
|---|---|
| 0 | 50 |
| 2 | 30 |
| 3 | 10 |
| 4 | 3 |
| 5 | 7 |
Solution 1:[1]
DF['T_days'].value_counts(normalize=True)*100)
worked. And after I turned it from a series to a DF to help the usage.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | buddemat |
