给现有的数据框添加索引。

huangapple go评论67阅读模式
英文:

Adding Index to existing DF

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学编程,对自己在做什么一无所知。我创建了这个pd.DataFrame,现在想回过头来添加一个索引,索引的值为('Count', 'Volume (R-AMT)', 'DV01')。

这是我尝试过的代码,但它不起作用。我看了一些YouTube视频和阅读了一些论坛。现在我在这里寻求帮助。谢谢!

英文:

I am new to coding and have no idea what I am doing. I made this pd.DataFrame and now want to go back and add an index with the values ('Count', 'Volume (R-AMT)', 'DV01')

df1 = pd.DataFrame(
{'RFQ Total - Excluding Pass': [RFQ_count, Vol_RAMT, DV01],
               'Hit Count': [hit_count, hit_count_volume, hit_count_DV01],
                'Hit Ratio': [hit_ratio_count, hit_ratio_volume, hit_ratio_DV01],
                'Cover Count': [1, 1, 1],
                'Cover Ratio': [1,1,1],
                'Tie Wins': [1,1,1],
                'Tie Away': [1,1,1],
                'Tie Ratio': [1,1,1],
                'Revenue': [1,1,1]})
index=['Count', 'Volume (R-AMT)', 'DV01']
df1

This is what I have tried, it does not work. I have watched a few youtube videos and read some forums. Now I am here asking for help. Thank!!

答案1

得分: 0

尝试在最后通过将底部的df1行替换为类似以下内容的方式创建一个新的数据框:df2 = pd.DataFrame(df1, index=index)。尝试打印df2以查看其结果。

英文:

try and create a new data frame at the end by replacing the df1 line at the bottom with something like. df2 = pd.DataFrame(df1, index=index). Try and print out df2 to see what it gives you.

huangapple
  • 本文由 发表于 2023年8月9日 00:10:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定