2017-11-29 9 views
1

私は以下のようなテキストファイルを持っており、それらをpandasデータフレームにインポートしたいと考えています。どうやってやるの?お手伝いありがとう。適切なヘッダーと場所でpandasデータフレームにtxtファイルを読み取るにはどうすればいいですか?

実際に最終的な目標は、テキストを書式設定して並べ替えることです。私はread_csvを試しましたが、データは間違った場所に置かれます。

私はあなたが必要と考えてい
 Date  Time Height(m) Time Height(m) Time Height(m) Time Height(m) 
01 01  0343 0.3  1009 1.6  1434 1.1  2048 2.7 
01 02  0435 0.1  1106 1.6  1521 1.1  2132 2.8 
01 03  0524 0.0  1157 1.6  1607 1.1  2217 2.8 
01 04  0613 0.0  1244 1.6  1654 1.1  2305 2.7 
01 05  0659 0.1  1330 1.6  1742 1.1  2352 2.5 
01 06  0744 0.2  1418 1.6  1833 1.2    
01 07  0041 2.3  0828 0.4  1508 1.7  1935 1.3 
01 08  0131 2.1  0912 0.6  1604 1.7  2106 1.3 
01 09  0228 1.8  0956 0.7  1702 1.8  2244 1.3 
01 10  0421 1.5  1043 0.9  1753 1.9    
01 11  0026 1.1  0618 1.4  1131 1.0  1832 2.0 
01 12  0154 0.9  0801 1.3  1219 1.1  1905 2.1 
01 13  0246 0.8  0913 1.4  1303 1.1  1935 2.2 
01 14  0326 0.6  0956 1.4  1343 1.2  2005 2.3 
01 15  0402 0.5  1030 1.4  1421 1.1  2036 2.4 
01 16  0436 0.4  1102 1.4  1457 1.1  2109 2.4 
01 17  0509 0.3  1132 1.5  1533 1.1  2142 2.5 
01 18  0539 0.3  1200 1.5  1610 1.1  2217 2.5 
01 19  0608 0.3  1227 1.5  1648 1.1  2252 2.4 
01 20  0634 0.4  1254 1.5  1728 1.1  2329 2.4 
01 21  0700 0.4  1325 1.5  1811 1.1    
01 22  0006 2.2  0729 0.5  1402 1.6  1859 1.1 
01 23  0047 2.1  0802 0.6  1445 1.6  2001 1.2 
01 24  0132 1.9  0839 0.7  1532 1.7  2129 1.1 
01 25  0230 1.7  0921 0.8  1622 1.8  2255 1.0 
01 26  0418 1.5  1012 1.0  1715 1.9    
01 27  0021 0.8  0621 1.4  1113 1.1  1807 2.1 
01 28  0140 0.6  0808 1.4  1220 1.1  1858 2.3 
01 29  0242 0.4  0920 1.4  1323 1.1  1948 2.5 
01 30  0336 0.2  1012 1.5  1420 1.1  2038 2.6 
01 31  0426 0.0  1056 1.5  1513 1.0  2127 2.7 
____________________________________________________________________________ 

    Date  Time Height(m) Time Height(m) Time Height(m) Time Height(m) 
02 01  0512 0.0  1136 1.6  1602 0.9  2216 2.7 
02 02  0555 0.0  1214 1.6  1650 0.9  2304 2.6 
02 03  0635 0.1  1250 1.7  1738 0.9  2351 2.4 
02 04  0712 0.3  1326 1.7  1826 0.9    
02 05  0035 2.2  0747 0.5  1400 1.7  1919 1.0 
02 06  0118 1.9  0818 0.7  1435 1.7  2026 1.1 
02 07  0203 1.7  0847 0.8  1516 1.8  2152 1.1 
02 08  0309 1.4  0917 1.0  1611 1.8  2338 1.0 
02 09  0605 1.3  0956 1.1  1720 1.9    
02 10  0135 0.9  0832 1.3  1056 1.2  1816 1.9 
02 11  0233 0.7  0924 1.3  1218 1.2  1901 2.0 
02 12  0310 0.6  0952 1.4  1318 1.2  1940 2.1 
02 13  0343 0.5  1014 1.4  1405 1.2  2017 2.2 
02 14  0414 0.4  1037 1.5  1447 1.1  2054 2.3 
02 15  0444 0.3  1059 1.5  1526 1.0  2132 2.4 
02 16  0512 0.3  1120 1.5  1605 0.9  2209 2.4 
02 17  0536 0.4  1139 1.6  1644 0.9  2248 2.3 
02 18  0558 0.4  1200 1.7  1725 0.8  2326 2.3 
02 19  0621 0.5  1223 1.7  1808 0.8    
02 20  0006 2.2  0648 0.6  1249 1.8  1856 0.8 
02 21  0048 2.0  0718 0.7  1319 1.8  1953 0.8 
02 22  0135 1.8  0750 0.8  1355 1.9  2108 0.8 
02 23  0242 1.5  0826 1.0  1445 1.9  2231 0.8 
02 24  0441 1.4  0909 1.1  1603 2.0  2359 0.7 
02 25  0708 1.3  1024 1.2  1729 2.1    
02 26  0124 0.5  0835 1.4  1204 1.2  1841 2.2 
02 27  0228 0.3  0922 1.5  1320 1.2  1941 2.4 
02 28  0320 0.2  0959 1.6  1421 1.0  2035 2.5 
____________________________________________________________________________ 
+0

コードを表示して、エラーを見つけやすくしてください... – Sosel

+0

ファイルを共有することは可能ですか?可能なのは可能なセパレータ、空白の数...希望される出力、e。最初の5行? – jezrael

+0

ここでファイルを共有するにはどうすればよいですか? Googleのドライブリンク? – JOHN

答えて

0

#read all values as str 
df = pd.read_csv(file, sep="\s+", dtype=str) 
#filter out Time values and NaN values 
df = df[(df['Date'] != 'Time') & (df['Date'].notnull())] 
#add first column to index 
df = df.set_index('Date', append=True) 
#add 0 to columns for MultiIndex in columns by split 
df = df.rename(columns={'Time':'Time.0','Height(m)':'Height(m).0'}) 
df.columns = df.columns.str.split('.', expand=True) 
#reshape and remove last level of MultiIndex 
df = df.stack().reset_index(level=-1, drop=True).reset_index() 
#last rename for nice columns names 
df = df.rename(columns={'level_0':'Month','Date':'Day'}) 

print (df.head(10)) 
    Month Day Height(m) Time 
0 01 01  0.3 0343 
1 01 01  1.6 1009 
2 01 01  1.1 1434 
3 01 01  2.7 2048 
4 01 02  0.1 0435 
5 01 02  1.6 1106 
6 01 02  1.1 1521 
7 01 02  2.8 2132 
8 01 03  0.0 0524 
9 01 03  1.6 1157 
0

私はこのようにしようとするだろう:

df = pd.read_csv('file.extension', sep=" ", header= 
['Date','Time','Height(m)','Time Height(m)','Time Height(m),'Time Height(m)']) 

あなたのファイルにあなたが2回ヘッダーを持っている場合、私はそれには、理解していませんでしたあなたのファイルを直接またはPythonで開き、その行を削除することができます。

+0

実際には12回あり、12ヶ月と同じです – JOHN

関連する問題