【问题标题】:Filtering or Querying Pandas MultiIndex Dataframe based on column values根据列值过滤或查询 Pandas MultiIndex Dataframe
【发布时间】:2022-12-04 12:30:28
【问题描述】:

I have a multi-index pandas DataFrame such as below, primarily indexed with DateTime object.

>>> type(feed_tail)
<class \'pandas.core.frame.DataFrame\'>

>>> feed_tail.index
DatetimeIndex([\'2022-11-11\', \'2022-11-14\', \'2022-11-15\', \'2022-11-16\',
           \'2022-11-17\', \'2022-11-18\', \'2022-11-21\', \'2022-11-22\',
           \'2022-11-23\', \'2022-11-24\'],
          dtype=\'datetime64[ns]\', name=\'Date\', freq=None)


>>> feed_tail.columns
MultiIndex([(       \'Close\', \'BALKRISIND.NS\'),
        (       \'Close\',        \'KSB.NS\'),
        (       \'SMA13\', \'BALKRISIND.NS\'),
        (       \'SMA13\',        \'KSB.NS\'),
        (\'SMA13gtClose\', \'BALKRISIND.NS\'),
        (\'SMA13gtClose\',        \'KSB.NS\'),
        (     \'MTDPerf\', \'BALKRISIND.NS\'),
        (     \'MTDPerf\',        \'KSB.NS\')],
       names=[\'Attributes\', \'Symbols\'])

>>> feed_tail
Attributes         Close                  SMA13           SMA13gtClose              MTDPerf
Symbols    BALKRISIND.NS   KSB.NS BALKRISIND.NS   KSB.NS BALKRISIND.NS KSB.NS BALKRISIND.NS KSB.NS
Date
2022-11-11       1889.45  1834.40       1933.03  1959.00         False  False         -3.73 -11.86
2022-11-14       1875.55  1848.60       1927.28  1944.42         False  False         -4.44 -11.18
2022-11-15       1963.20  1954.15       1928.51  1938.12          True   True          0.02  -6.11
2022-11-16       1956.30  1969.75       1929.43  1933.65          True   True         -0.33  -5.36
2022-11-17       1978.35  1959.55       1932.08  1927.51          True   True          0.79  -5.85
2022-11-18       1972.75  1917.90       1932.85  1914.94          True   True          0.51  -7.85
2022-11-21       1945.80  1874.70       1932.80  1902.38          True  False         -0.86  -9.93
2022-11-22       1950.30  1882.85       1932.60  1892.80          True  False         -0.63  -9.54
2022-11-23       1946.60  1930.90       1936.52  1893.97          True   True         -0.82  -7.23
2022-11-24       1975.40  1925.80       1941.11  1901.10          True   True          0.64  -7.47

I am trying to access/filter the dataframe into another dataframe, for every datetime index in sequence where SMA13gtClose column is True but seems like I am failing at understanding the datamodel here. Quest is to iterate over the datetime index in sequence, and get dataframes where the Symbols\' SMA13gtClose is True or SMA13 is greater than Close with in the same dataframe and then go over the filtered/queried dataframe for further processing within the loop.

Any help towards unravelling this further is sincerely appreciated.

Thank you

    标签: python pandas dataframe numpy multi-index


    【解决方案1】:
    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2019-06-15
    • 2019-05-27
    • 2017-09-14
    • 2019-04-29
    • 2016-11-06
    相关资源
    最近更新 更多