2017-07-29 8 views
0

ウィキペディアの一部のデータをコピーして貼り付けて作成するcsvファイルを読み込む必要があります。データは、その州の原点によってグループ分けされた大学のリストです。 私がしようとしているのは、このデータをpandasデータフレームにインポートすることです。インデックスは状態の名前です。しかし、read_csvを使用してcsvをインポートすると、データは1次元であり、状態名は大学の名前と同じ列にあります。 このデータフレームから、最初の列から状態を抽出し、それらをインデックスとして使用する必要があります。それをどうやって行うかわからない。 私はfor/ifループで状態名のリストを試すことができると思った。より速くよりエレガントな方法があるかもしれません。 提案がありますか?pandas read_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) 

感謝:

これはcsvファイルがどのように見えるかです!

+0

データのサンプルを置くことはできますか? – MedAli

+0

ちょうどデータの貼り付け部分 – Jemba88

+0

ありがとう@ayhan!私はそこからそれを取り除いた – Jemba88

答えて

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) 
+0

残念ながら、csvには1列しかありません。 – Jemba88

+0

この方法は、いくつかの調整をしてきた可能性があります。その状態の問題は、「状態」の列には[編集]が続くデータのみがあり、他の列には()の残りのデータがあるはずです。州の列には大学の状態が変わるまで同じ値を含める必要があります。たとえば、「州」欄の最初の8行にはアラバマ州が8回あり、その州の「大学柱」に記載されている8つの大学に対応していなければなりません。 別のスレッドで回答が見つかりました。あなたのお時間をありがとう! – Jemba88

関連する問題