import pandas as pd
import numpy as np
f = pd.read_csv('151101.mnd',skiprows=33, sep ='\s+',chunksize=30)
data = pd.concat(f)
data = data.convert_objects(convert_numeric=True)
print data.head()
print ''
height = data['#']
wspd = data['z']
hub = np.where(height==80)
print np.where(height==80)
文件的开头部分:
# z speed dir W sigW bck error
0 30 5.05 333.0 0.23 0.13 144000 0 NaN
1 40 5.05 337.1 -0.02 0.14 7690 0 NaN
2 50 5.03 338.5 0.00 0.15 4830 0 NaN
3 60 6.21 344.3 -0.09 0.18 6130 0 NaN
4 70 5.30 336.5 0.01 0.21 158000 0 NaN
输出(高度列= 80的索引):
(array([ 5, 37, 69, 101, 133, 165, 197, 229, 261, 293, 325,
357, 389, 421, 453, 485, 517, 549, 581, 613, 645, 677,
709, 741, 773, 805, 837, 869, 901, 933, 965, 997, 1029,
1061, 1093, 1125, 1157, 1189, 1221, 1253, 1285, 1317, 1349, 1381,
1413, 1445, 1477, 1509, 1541, 1573, 1605, 1637, 1669, 1701, 1733,
1765, 1797, 1829, 1861, 1893, 1925, 1957, 1989, 2021, 2053, 2085,
2117, 2149, 2181, 2213, 2245, 2277, 2309, 2341, 2373, 2405, 2437,
2469, 2501, 2533, 2565, 2597, 2629, 2661, 2693, 2725, 2757, 2789,
2821, 2853, 2885, 2917, 2949, 2981, 3013, 3045, 3077, 3109, 3141,
3173, 3205, 3237, 3269, 3301, 3333, 3365, 3397, 3429, 3461, 3493,
3525, 3557, 3589, 3621, 3653, 3685, 3717, 3749, 3781, 3813, 3845,
3877, 3909, 3941, 3973, 4005, 4037, 4069, 4101, 4133, 4165, 4197,
4229, 4261, 4293, 4325, 4357, 4389, 4421, 4453, 4485, 4517, 4549,
4581], dtype=int64),)
所以我想找到wspd,data。['z'],其中height,data。['#'] = 80并将其存储为变量。我该怎么做呢?我试图做一个np.where(height = 80)并将其存储为变量'hub'但是当我在hub的索引处使用wspd时,wspd [hub]我收到错误。 ValueError:只能使用MultiIndex进行元组索引。有更简单的方法吗?
答案 0 :(得分:2)
使用示例:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A': [2,3,2,5],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']},
index=[0, 1, 2, 3])
print df1
c = df1[df1.A == 2].index # get all the indices where value is 2 in column 'A'
d= df1.iloc[c,] #Subset dataframe with only these row indices
d_values = df1.iloc[c,1].values #to return an array of values in column 'B'/2nd column.
输出
array(['B0', 'B2'], dtype=object)
在你的情况下:
hub = data[data['#'] == 80].index
new_data = data.iloc[hub,]
要仅获取wspd
值,请改用:
new_data = data.iloc[hub,1].values #assuming that it is the 2nd column always, this will return an array.