如何在 pandas 中设置第一列和第一行作为索引? [英] How to set in pandas the first column and row as index?

查看:246
本文介绍了如何在 pandas 中设置第一列和第一行作为索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读入 CSV 文件时,我可以说 pd.read_csv('my.csv', index_col=3) 并将第三列设置为索引.

When I read in a CSV, I can say pd.read_csv('my.csv', index_col=3) and it sets the third column as index.

如果我的内存中有一个 Pandas 数据框,我该怎么做?我怎么能说将第一行也用作索引?第一列第一行是字符串,矩阵的其余部分是整数.

How can I do the same if I have a pandas dataframe in memory? And how can I say to use the first row also as an index? The first column and row are strings, rest of the matrix is integer.

推荐答案

不管行数多少都可以试试

You can try this regardless of the number of rows

df = pd.read_csv('data.csv', index_col=0)

这篇关于如何在 pandas 中设置第一列和第一行作为索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆