python - 如何合并 Pandas 中的列?

标签 python pandas dataframe

我有一个数据框index_crisis,并且想要创建一个新列,其中当索引达到局部峰值时包含 1,其他情况下包含 0。 我不知道如何继续我的代码。列表峰值位置为: [ 2 7 9 13 16 18 21] 但通过 month[peak_locations] 我得到了峰值的月份。

    Date           Index 
38  2007-06-01  -0.56
39  2007-07-01  -0.36
40  2007-08-01  0.68
41  2007-09-01  0.24
42  2007-10-01  0.22
43  2007-11-01  0.89
44  2007-12-01  0.95
45  2008-01-01  1.53
46  2008-02-01  1.01
47  2008-03-01  1.73
48  2008-04-01  1.39
49  2008-05-01  0.96
50  2008-06-01  1.26
51  2008-07-01  2.37
52  2008-08-01  1.57
53  2008-09-01  2.95
54  2008-10-01  5.7
55  2008-11-01  5.29
56  2008-12-01  5.42
57  2009-01-01  4.99
58  2009-02-01  4.45
59  2009-03-01  4.59
60  2009-04-01  4.2
61  2009-05-01  3.12
62  2009-06-01  1.85

我的预期输出是一列虚拟,如下所示:

0
0
1
0
0
0
0
1
0
1
0
0
0
1
0
0
1
0
1
0
0
1
0
0
0
df = pd.read_csv("index_crisis.csv", parse_dates=True)

df['Date'] = pd.to_datetime(df['Date'])
df['Date'] = pd.PeriodIndex(df.Date, freq='M').strftime("%b %Y")


data = df['Index'].values
doublediff = np.diff(np.sign(np.diff(data)))
peak_locations = np.where(doublediff == -2)[0] + 1

最佳答案

idx = df.iloc[peak_locations].index
df['dummy'] = np.where(df.index.isin(idx), 1, 0)
    Date    Index   dummy
38  Jun 2007    -0.56   0
39  Jul 2007    -0.36   0
40  Aug 2007    0.68    1
41  Sep 2007    0.24    0
42  Oct 2007    0.22    0
43  Nov 2007    0.89    0
44  Dec 2007    0.95    0
45  Jan 2008    1.53    1
46  Feb 2008    1.01    0
47  Mar 2008    1.73    1
48  Apr 2008    1.39    0
49  May 2008    0.96    0
50  Jun 2008    1.26    0
51  Jul 2008    2.37    1
52  Aug 2008    1.57    0
53  Sep 2008    2.95    0
54  Oct 2008    5.7     1
55  Nov 2008    5.29    0
56  Dec 2008    5.42    1
57  Jan 2009    4.99    0
58  Feb 2009    4.45    0
59  Mar 2009    4.59    1
60  Apr 2009    4.2     0
61  May 2009    3.12    0
62  Jun 2009    1.85    0

关于python - 如何合并 Pandas 中的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57473209/

相关文章:

sql-server - Pandas 和 SQLAlchemy : df. to_sql() with SQLAlchemy 2.0 future=True 在使用来自 engine.begin() 的连接时抛出错误

python - 当在每一行中需要使用整个数据进行比较时,在 Pandas 中使用矢量化

python - 如何在 pandas 中使用条件格式?

python - 改进非英语文本的 NER 标签结果

python - Python Tkinter 中.xxxxxxx 的含义是什么

python - 如何旋转 matplotlib map ?

python - 计算*滚动* Pandas 系列的最大回撤

python - pandas python 中的日期范围问题

python - 在 Pandas 的两列中按名称拆分和附加

python - 从 Pandas 数据框中过滤数据