英文:
Switching dataframe integers to string so i can add text like '$' and '5.78 / Million' into the dataframe
问题
我创建了一个包含所有必要信息的数据框,但是我的经理希望我删除除 col['REVENUE'] x index['Volume'] 外所有条目的小数部分。也就是保留 5.78 并删除其他所有内容的尾随零。
我还需要将像 '$' 和 'mm' 这样的文本添加到一些值中,例如 "5,388 MM" 和 "$2,965,773"。
说实话,我对接下来要做什么感到迷茫。感谢您的帮助。
如果您能把它分解成小步骤,即使只是问题的一部分,我将非常感激。
我尝试将一些值转换为字符串,但当我尝试打印数据框时,这会给我一个错误消息。我包含了一些代码,希望能更清楚一些。如果有什么可以澄清这个问题的方法,请告诉我!!!
RFQ_count = df['STATUS'].count()
# RFQ_count = str(RFQ_count)
# print(type(RFQ_count))
Vol_RAMT = df['R-AMT'].sum()
Vol_RAMT = int(round(Vol_RAMT))
DV01 = df['DV01'].sum()
DV01 = int(round(DV01))
DF_data = {
'Total': [RFQ_count, Vol_RAMT, DV01],
'Count': [hit_count, hit_count_volume, hit_ount_DV01],
'Ratio1 %': [hit_ratio_count, hit_ratio_volume, hit_ratio_DV01],
'Count': [cover_plus_count, cover_plus_volume, cover_plus_DV01],
'Ratio2 %': [cover_count_ratio, cover_volume_ratio, cover_DV01_ratio],
'Wins': [tie_win_count, tie_win_volume, tie_win_DV01],
'Away': [tie_away_count, tie_away_volume, tie_away_DV01],
'Ratio3 %': [int(round((tie_win_count / tie_away_count) * 100)),
int(round((tie_win_volume / tie_away_volume) * 100)),
int(round((tie_win_DV01 / tie_away_DV01) * 100))],
'Revenue': [rev_count, rev_vol, rev_DV01],
}
index_labels=["Count", "Volume", "DV01"]
df1 = pd.DataFrame(DF_data, index=index_labels).round(2).style.format("{:,}")
# df1['RFQ Total'] = df1['RFQ Total'].astype(str) + '$'
print(type(DF_data))
df1
<details>
<summary>英文:</summary>
I made a dataframe with all the necessary information, however, my manager wants me to remove decimals from all entries except col['REVENUE'] x index['Volume']. Aka keep 5.78 and remove the trailing zero from everything else.
I also need to add in text like '$' and 'mm' to some values like "5,388 MM" and "$2,965,773."
To put it nicely, I am lost in the sauce about what to do now. Thanks for your help.
If you could please break it down to baby steps, even if its just parts of the problem, I would be immensly grateful.
I tried converting some values into strings, but that then gave me an error message when i tried to print the df. I included some of the code up to hopefully make things a bit more clear. Please let me know if there is anything I can do to clarify this question!!!
RFQ_count = df['STATUS'].count()
# RFQ_count = str(RFQ_count)
# print(type(RFQ_count))
Vol_RAMT = df['R-AMT'].sum()
Vol_RAMT = int(round(Vol_RAMT))
DV01 = df['DV01'].sum()
DV01 = int(round(DV01))
DF_data = {
'Total': [RFQ_count, Vol_RAMT, DV01],
'Count': [hit_count, hit_count_volume, hit_ount_DV01],
'Ratio1 %': [hit_ratio_count, hit_ratio_volume, hit_ratio_DV01],
'Count': [cover_plus_count, cover_plus_volume, cover_plus_DV01],
'Ratio2 %': [cover_count_ratio, cover_volume_ratio, cover_DV01_ratio],
'Wins': [tie_win_count, tie_win_volume, tie_win_DV01],
'Away': [tie_away_count, tie_away_volume, tie_away_DV01],
'Ratio3 %': [int(round((tie_win_count / tie_away_count) * 100)),
int(round((tie_win_volume / tie_away_volume) * 100)),
int(round((tie_win_DV01 / tie_away_DV01) * 100))],
'Revenue': [rev_count, rev_vol, rev_DV01],
}
index_labels=["Count", "Volume", "DV01"]
df1 = pd.DataFrame(DF_data, index=index_labels).round(2).style.format("{:,}")
# df1['RFQ Total'] = df1['RFQ Total'].astype(str) + '$'
print(type(DF_data))
df1`
[![This is the output table](https://i.stack.imgur.com/JWlij.png)](https://i.stack.imgur.com/JWlij.png)
</details>
# 答案1
**得分**: 2
在没有其他上下文的情况下,最好的做法是避免在代码中进行转换,而是使用格式化器。这个字符串可以放在一个预格式化的块中。
```python
import locale
from functools import partial
import pandas as pd
df = pd.DataFrame(
data={
'Total': (785, 5_388, 2_965_773),
'Count': (518, 3_729, 2_120_275),
'Ratio1': (0.27, 0.30, 0.31),
'Ratio2': (0.66, 0.69, 0.71),
'Wins': (100, 906, 419_929),
'Away': (82, 658, 310_545),
'Ratio3': (1.22, 1.38, 1.35),
'Revenue': (9_268.00, 578.00, 1_000.00),
},
index=('Count', 'Volume', 'DV01'),
)
locale.setlocale(locale.LC_MONETARY, '')
print(
df.to_string(
formatters={
'Total': '{:,.0f}'.format,
'Count': '{:,.0f}'.format,
'Wins': '{:,.0f}'.format,
'Away': '{:,.0f}'.format,
'Ratio1': '{:.0%}'.format,
'Ratio2': '{:.0%}'.format,
'Ratio3': '{:.0%}'.format,
'Revenue': partial(locale.currency, grouping=True),
}
)
)
Total Count Ratio1 Ratio2 Wins Away Ratio3 Revenue
Count 785 518 27% 66% 100 82 122% $9,268.00
Volume 5,388 3,729 30% 69% 906 658 138% $578.00
DV01 2,965,773 2,120,275 31% 71% 419,929 310,545 135% $1,000.00
英文:
With no other context, the preferable thing to do is avoid converting in the frame itself, and use formatters instead. This string can be put in a PDF in a pre-formatted block.
import locale
from functools import partial
import pandas as pd
df = pd.DataFrame(
data={
'Total': (785, 5_388, 2_965_773),
'Count': (518, 3_729, 2_120_275),
'Ratio1': (0.27, 0.30, 0.31),
'Ratio2': (0.66, 0.69, 0.71),
'Wins': (100, 906, 419_929),
'Away': (82, 658, 310_545),
'Ratio3': (1.22, 1.38, 1.35),
'Revenue': (9_268.00, 578.00, 1_000.00),
},
index=('Count', 'Volume', 'DV01'),
)
locale.setlocale(locale.LC_MONETARY, '')
print(
df.to_string(
formatters={
'Total': '{:,.0f}'.format,
'Count': '{:,.0f}'.format,
'Wins': '{:,.0f}'.format,
'Away': '{:,.0f}'.format,
'Ratio1': '{:.0%}'.format,
'Ratio2': '{:.0%}'.format,
'Ratio3': '{:.0%}'.format,
'Revenue': partial(locale.currency, grouping=True),
}
)
)
Total Count Ratio1 Ratio2 Wins Away Ratio3 Revenue
Count 785 518 27% 66% 100 82 122% $9,268.00
Volume 5,388 3,729 30% 69% 906 658 138% $578.00
DV01 2,965,773 2,120,275 31% 71% 419,929 310,545 135% $1,000.00
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论