pandas read_csv方法从lis构建索引

2024-09-26 22:53:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要阅读一个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)

非常感谢!在


Tags: ofcsv数据名称大学stateschoollaw
1条回答
网友
1楼 · 发布于 2024-09-26 22:53:09

pandas.read_csv文档中所述,您可以使用index_col来定义csv文件中要用作索引的列。在

对于您的特定情况,这里有一个工作代码示例,您需要将数据放入一个文件中,然后编辑下面的代码来读取该文件

import pandas as pd


# read your data into a list of lines 
with open("/tmp/data.txt", "rb") as myfile:
  data= myfile.readlines()

# strip whitespaces from each line 
data = [i.strip() for i in data]

# split each line with space to a list of words 
data = [i.split(" ") for i in data]

# create a list of lists where 
# each list contains the state name in the first element 
# and the other words in the second element 
data = [[i[0], " ".join(i[1:])] for i in data]

# create a data frame from the prepared data 
data = pd.DataFrame(data, columns=["state", "university"])

# convert the state column to the dataframe index 
data = data.set_index("state")

# see the results 
print(data.head())

结果如下:

^{pr2}$

相关问题 更多 >

    热门问题