parse
html
とpandas
という素晴らしい方法が見つかりました。私のデータは、奇妙な形式のものです(下記参照)。私はこのデータを2つの別々のdataframes
に分割したいと思います。Pandas DataFrame [cell =(label、value)]、2つの別々のデータフレームに分割
各cell
が,
によって分離されている様子がわかり... は、これらの細胞のすべてを分割し、2つのデータフレーム、ラベル用とカッコ内(value)
ための1つを作成するために、任意の本当に効率的な方法はありますか?
NumPy
は、これらすべてのufuncs
を持って、彼らはDF.as_matrix()
とnp.array
に変換することができますので、私はstring
dtypes
上でそれらを使用することができます方法はありますか?私はfor loops
をクリアしようとしていますが、すべてのインデックスを繰り返して空の配列を埋め込むことができますが、それはかなり野蛮です。私はところでBeaker Notebook
を使用してい
、それは(強く推奨)
#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)種; 及び(2)あなたはこのようにそれを行うことができます
あなたは、所望の出力のDFを投稿することができますか?現在、あなたは何を達成したいのかはっきりしていません – MaxU
さて、私はそれを本当に素早く計算することができます。 –
それは今のところ@MaxU –