2016-04-19 12 views
1

parsehtmlpandasという素晴らしい方法が見つかりました。私のデータは、奇妙な形式のものです(下記参照)。私はこのデータを2つの別々のdataframesに分割したいと思います。Pandas DataFrame [cell =(label、value)]、2つの別々のデータフレームに分割

cell,によって分離されている様子がわかり... は、これらの細胞のすべてを分割し、2つのデータフレーム、ラベル用とカッコ内(value)ための1つを作成するために、任意の本当に効率的な方法はありますか?

NumPyは、これらすべてのufuncsを持って、彼らはDF.as_matrix()np.arrayに変換することができますので、私はstringdtypes上でそれらを使用することができます方法はありますか?私はfor loopsをクリアしようとしていますが、すべてのインデックスを繰り返して空の配列を埋め込むことができますが、それはかなり野蛮です。私はところでBeaker Notebookを使用してい

、それは(強く推奨)

enter image description here

#Set URL Destination 
url = "http://www.reef.org/print/db/stats" 

#Process raw table 
DF_raw = pd.pandas.read_html(url)[0] 

#Get start/end indices of table 
start_label = "10 Most Frequent Species"; start_idx = (DF_raw.iloc[:,0] == start_label).argmax() 
end_label = "Top 10 Sites for Species Richness"; end_idx = (DF_raw.iloc[:,0] == end_label).argmax() 

#Process table 
DF_freqSpecies = pd.DataFrame(
           DF_raw.as_matrix()[(start_idx + 1):end_idx,:], 
           columns = DF_raw.iloc[0,:] 
) 
DF_freqSpecies 

#Split these into 2 separate DataFrames 

本当にクールだここで、このようなを行うための私の素朴な方法です:

import re 
DF_species = pd.DataFrame(np.zeros_like(DF_freqSpecies),columns=DF_freqSpecies.columns) 
DF_freq = pd.DataFrame(np.zeros_like(DF_freqSpecies).astype(str),columns=DF_freqSpecies.columns) 

dims = DF_freqSpecies.shape 
for i in range(dims[0]): 
    for j in range(dims[1]): 
     #Parse current dataframe 
     species, freq = re.split("\s\(\d",DF_freqSpecies.iloc[i,j]) 
     freq = float(freq[:-1]) 
     #Populate split DataFrames 
     DF_species.iloc[i,j] = species 
     DF_freq.iloc[i,j] = freq 

私はこれらの2つのデータフレームをしたいです私の出力として:

(1)種; enter image description here 及び(2)あなたはこのようにそれを行うことができます enter image description here

+0

あなたは、所望の出力のDFを投稿することができますか?現在、あなたは何を達成したいのかはっきりしていません – MaxU

+0

さて、私はそれを本当に素早く計算することができます。 –

+0

それは今のところ@MaxU –

答えて

2

を周波数:

DF1:

In [182]: df1 = DF_freqSpecies.replace(r'\s*\(\d+\.*\d*\)', '', regex=True) 

In [183]: df1.head() 
Out[183]: 
0 Tropical Western Atlantic California, Pacific Northwest and Alaska \ 
0     Bluehead       Copper Rockfish 
1     Blue Tang         Lingcod 
2  Stoplight Parrotfish      Painted Greenling 
3  Bicolor Damselfish       Sunflower Star 
4    French Grunt       Plumose Anemone 

0      Hawaii Tropical Eastern Pacific \ 
0    Saddle Wrasse   King Angelfish 
1 Hawaiian Whitespotted Toby   Mexican Hogfish 
2  Raccoon Butterflyfish    Barberfish 
3   Manybar Goatfish   Flag Cabrilla 
4    Moorish Idol Panamic Sergeant Major 

0    South Pacific Northeast US and Eastern Canada \ 
0   Regal Angelfish       Cunner 
1 Bluestreak Cleaner Wrasse     Winter Flounder 
2   Manybar Goatfish      Rock Gunnel 
3    Brushtail Tang       Pollock 
4  Two-spined Angelfish     Grubby Sculpin 

0 South Atlantic States  Central Indo-Pacific 
0   Slippery Dick    Moorish Idol 
1  Belted Sandfish  Three-spot Dascyllus 
2  Black Sea Bass Bluestreak Cleaner Wrasse 
3    Tomtate  Blacklip Butterflyfish 
4    Cubbyu  Clark's Anemonefish 

とDF2

In [193]: df2 = DF_freqSpecies.replace(r'.*\((\d+\.*\d*)\).*', r'\1', regex=True) 

In [194]: df2.head() 
Out[194]: 
0 Tropical Western Atlantic California, Pacific Northwest and Alaska Hawaii \ 
0      85          54.6  92 
1      84.8          53.2 85.8 
2      81          50.8 85.7 
3      79.9          50.2 85.7 
4      74.8          49.7 82.9 

0 Tropical Eastern Pacific South Pacific Northeast US and Eastern Canada \ 
0      85.7   79       67.4 
1      82.5   77.3       46.6 
2      75.2   73.9       26.2 
3      68.9   73.3       25.2 
4      67.9   72.8       23.7 

0 South Atlantic States Central Indo-Pacific 
0     79.7     80.1 
1     78.5     75.6 
2     78.5     73.5 
3     72.7     71.4 
4     65.7     70.2 

RegEx debugging and explanation:

私たちは基本的にカッコ内の数字を除いて、すべてのものを削除する:

(\d+\.*\d*) - (1)グループ - 括弧

.*\((\d+\.*\d*)\).*で私たちの数 - - 全部 - 'の前に何でもそれが私たちの数

\((\d+\.*\d*)\)です(」、 『(』、私たちの数、 『)』、それが基で置換されるセル

の最後まで何が(1) - 私たちの数

+0

2行です!驚くばかり。 * \((\ d + \。* \ d *)\)。 '、r' \ 1 "'私はもう一つ理解しますが、これは混乱します。 –

+0

@ O.rka、RegExパートの説明を追加しました – MaxU

+1

@MaxU @ O.rka最初の置き換えは、カッコで囲まれたものすべてを食べます。例えば、 'DF_freqSpecies.loc [9、 'Hawaii']'。 – ptrj

関連する問題