HM14
HM14

Reputation: 709

indexing column value by row value with pandas

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)

Beginning Part of the File:

    #     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

Output (indices Where height column = 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),)

So I want to find the wspd, data.['z'], where the height, data.['#']=80 and store that as a variable. How do I do this? I tried to do a np.where(height=80) and store that as a variable 'hub' but when I take wspd at the indices of hub, wspd[hub] I get an error. ValueError: Can only tuple-index with a MultiIndex. Is there an easier way to do this?

Upvotes: 1

Views: 1428

Answers (1)

Hypothetical Ninja
Hypothetical Ninja

Reputation: 4077

Example usage :

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.  

Output:

array(['B0', 'B2'], dtype=object)

In your case:

hub = data[data['#'] == 80].index  
new_data =  data.iloc[hub,]  

To get the wspd values only, use this instead:

new_data =  data.iloc[hub,1].values #assuming that it is the 2nd column always, this will return an array.

Upvotes: 2

Related Questions