[英]How (with apply) to select and copy specific columns in a Dataframe according to index or another column
I already asked my question but it was not enough accurate in its description. 我已经问过我的问题,但是描述不够准确。 Smart people in this forum already proposed solutions, but I forgot(sorry) to precise that if there were zeros in the relevant columns, they should be kept.
这个论坛上的聪明人已经提出了解决方案,但我忘记了(抱歉),如果相关列中有零,应该保留它们。
Hello I have a dataframe like below 您好我有一个如下数据框
2014 2015 2016 2017 2018 2019
2014 10 20 30 40 0 5
2015 0 0 200 0 100 0
2016 0 0 200 140 35 10
2017 0 0 0 20 0 12
I need to have a result like this: 我需要这样的结果:
yearStart yearStart+1 yearStart+2 yearStart+3 yearStart+4
0 10 20 30 40 0
1 0 200 0 100 0
2 200 140 35 10 0
3 20 0 12 0 0
The idea is to select in each row, the columns between two dates: 想法是在每一行中选择两个日期之间的列:
index and index +delta,with delta a parameter (in this example 4) to put them in a dataframe. index和index + delta,其中delta是一个参数(在此示例中为4),以将其放入数据帧中。
With iterrows(), it takes too much time. 使用iterrows(),会花费太多时间。
I tried with 我尝试过
df1 = df.apply(lambda x: pd.Series(x[x.keys()>=x.index],1)).fillna(0).astype(int)
but it doesn't work: 但它不起作用:
TypeError: ('Index(...) must be called with a collection of some kind,
1 was passed', 'occurred at index 2014')
Thank you 谢谢
One of the ways would be 一种方法是
In [1010]: def yearmove(x):
...: idx = x.index.astype(int)
...: idx = idx - x.name
...: mask = idx >= 0
...: idx = 'yearStart' + idx.astype(str)
...: return pd.Series(x.values[mask], index=idx[mask])
...:
In [1011]: df.apply(yearmove, 1).fillna(0).astype(int)
Out[1011]:
yearStart0 yearStart1 yearStart2 yearStart3 yearStart4 yearStart5
2014 10 20 30 40 0 5
2015 0 200 0 100 0 0
2016 200 140 35 10 0 0
2017 20 0 12 0 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.