【问题标题】:I want to compare values in a dataframe column and report the index for the value that satisfy a conditional argument?我想比较数据框列中的值并报告满足条件参数的值的索引?
【发布时间】:2020-05-04 14:11:23
【问题描述】:
Unnamed: 4  GDP in billions of chained 2009 dollars.1
214 2000q1  12359.1
215 2000q2  12592.5
216 2000q3  12607.7
217 2000q4  12679.3
218 2001q1  12643.3
219 2001q2  12710.3
220 2001q3  12670.1
221 2001q4  12705.3
222 2002q1  12822.3
223 2002q2  12893.0
224 2002q3  12955.8
225 2002q4  12964.0
226 2003q1  13031.2
227 2003q2  13152.1
228 2003q3  13372.4
229 2003q4  13528.7
230 2004q1  13606.5
231 2004q2  13706.2
232 2004q3  13830.8
233 2004q4  13950.4
234 2005q1  14099.1
235 2005q2  14172.7
236 2005q3  14291.8
237 2005q4  14373.4
238 2006q1  14546.1
239 2006q2  14589.6
240 2006q3  14602.6
241 2006q4  14716.9
242 2007q1  14726.0
243 2007q2  14838.7
... ... ...
250 2009q1  14375.0
251 2009q2  14355.6
252 2009q3  14402.5
253 2009q4  14541.9
254 2010q1  14604.8
255 2010q2  14745.9
256 2010q3  14845.5
257 2010q4  14939.0
258 2011q1  14881.3
259 2011q2  14989.6
260 2011q3  15021.1
261 2011q4  15190.3
262 2012q1  15291.0
263 2012q2  15362.4
264 2012q3  15380.8
265 2012q4  15384.3
266 2013q1  15491.9
267 2013q2  15521.6
268 2013q3  15641.3
269 2013q4  15793.9
270 2014q1  15747.0
271 2014q2  15900.8
272 2014q3  16094.5
273 2014q4  16186.7
274 2015q1  16269.0
275 2015q2  16374.2
276 2015q3  16454.9
277 2015q4  16490.7
278 2016q1  16525.0
279 2016q2  16583.1

我有上面的数据框。我想比较 GDP 列中的值(以 2009 年的十亿美元为单位)。1 并报告列的值连续小于列值的行的索引和值(对于高于它的两个值)。我正在使用以下代码,但我没有得到结果

datan = pd.read_excel('gdplev.xls', skiprows = 5)
datan.drop(datan.iloc[0:230, 0:4], inplace = True, axis = 1)
datan = datan[214:]
datan = datan.drop(['GDP in billions of current dollars.1', 'Unnamed: 7'], axis = 1)
datan
for item in datan['GDP in billions of chained 2009 dollars.1']:
    if item > item+1 and item+1 > item+2:
        print(item+2)

请帮忙

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    我建议如下:

    # First I reproduce a similar DataFrame than yours
    import pandas as pd
    import numpy as np
    np.random.seed(123)
    df = pd.DataFrame({"quarter" : pd.date_range("2000q1", freq="Q", periods = 10),
                        "gdp": np.random.rand(10)*10000})
    df["quarter"] = pd.Series(df["quarter"].dt.year).astype("str") + "q" + pd.Series(df["quarter"].dt.quarter).astype("str")
    
    # Then I create two columns that are the lags of gdp
    df["gdpN_1"] = df["gdp"].shift()
    df["gdpN_2"] = df["gdpN_1"].shift()
    # I create a top when gdp is below gdp at past quarter and the quarter before that
    df["top"] = (df["gdp"] < df["gdpN_1"]) & (df["gdp"] < df["gdpN_2"])
    # I only select rows for which top is True
    new_df = df.loc[df["top"], ["quarter", "gdp"]]
    

    new_df 的结果是:

      quarter          gdp
    2  2000q3  2268.514536
    5  2001q2  4231.064601
    8  2002q1  4809.319015
    9  2002q2  3921.175182
    

    【讨论】:

      猜你喜欢
      • 2020-02-07
      • 2019-07-13
      • 2016-03-17
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      相关资源
      最近更新 更多