我需要阅读一个csv文件,通过复制和粘贴维基百科的一些数据来创建。数据是按大学的起源国分组的大学名单。 我要做的是将这些数据导入到pandas数据框中,其中的索引是各州的名称。但是,当我使用read_csv导入csv时,数据是一维的,州名称与大学名称在同一列中。 从这个数据帧中,我现在应该从第一列中提取状态并将它们用作索引。但不知道该怎么做。 我想我可以尝试使用一个包含状态名列表的for/if循环;但可能有一种更快、更优雅的方法。 有什么建议吗?在
csv文件如下所示:
Alabama[edit]
Auburn (Auburn University, Edward Via College of Osteopathic Medicine)[14]
Birmingham (University of Alabama at Birmingham, Birmingham School of Law, Cumberland School of Law, Miles Law School)[15]
Dothan (Fortis College, Troy University Dothan Campus, Alabama College of Osteopathic Medicine)
Florence (University of North Alabama)
Homewood (Samford University)
Huntsville (University of Alabama, Huntsville)
Jacksonville (Jacksonville State University)[16]
Livingston (University of West Alabama)[16]
Mobile (University of South Alabama)[17]
Montevallo (University of Montevallo, Faulkner University)[16]
Montgomery (Alabama State University, Huntingdon College, Auburn University at
Montgomery, H. Councill Trenholm State Technical College, Faulkner University)
Troy (Troy University)[16]
Tuscaloosa (University of Alabama, Stillman College, Shelton State)[18][19]
Tuskegee (Tuskegee University)[20]
Alaska[edit]
Anchorage[21] (University of Alaska Anchorage)
Fairbanks (University of Alaska Fairbanks)[16]
Juneau (University of Alaska Southeast)
Ketchikan (University of Alaska Southeast-extended campus)
Sitka (University of Alaska Southeast-extended campus)
非常感谢!在
如pandas.read_csv文档中所述,您可以使用
index_col
来定义csv文件中要用作索引的列。在对于您的特定情况,这里有一个工作代码示例,您需要将数据放入一个文件中,然后编辑下面的代码来读取该文件
结果如下:
^{pr2}$相关问题 更多 >
编程相关推荐