pandas read_csv方法从列表构建索引

时间:2017-07-29 08:43:27

标签: python pandas csv dataframe

我需要通过复制和粘贴来自维基百科的一些数据来阅读创建的csv文件。数据是按其原籍国分组的大学列表。 我想要做的是在pandas数据帧中导入这些数据,其中索引是状态的名称。但是,当我使用read_csv导入csv时,数据是1维的,状态名称与大学名称在同一列中。 从这个数据帧我现在应该从第一列中提取状态并将它们用作索引。不知道该怎么做。 我以为我可以尝试使用带有州名列表的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)

非常感谢!

1 个答案:

答案 0 :(得分:0)

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())

结果如下:

                                                      university
state                                                           
Alabama[edit]                                                   
Auburn         (Auburn University, Edward Via College of Oste...
Birmingham     (University of Alabama at Birmingham, Birmingh...
Dothan         (Fortis College, Troy University Dothan Campus...
Florence                           (University of North Alabama)