简体   繁体   English

如何(使用)根据索引或另一列选择和复制数据框中的特定列

[英]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.

相关问题 Pandas 如何将一列复制到另一个具有相似索引的 dataframe - Pandas how to copy a column to another dataframe with similar index 如何根据列条件将选定的列从数据框中复制到另一个 - How to copy selected columns from a dataframe to another based on a column condition 如何从另一个 dataframe 将索引应用于 dataframe - how to apply an index to a dataframe from another dataframe 如何使用 apply 和 lambda 根据与第二个 dataframe 中的另一列匹配的索引将值设置为 dataframe 列 - How to use apply and lambda to set value to a dataframe column based on the index matching another column in a second dataframe 根据特定规则将Pandas数据框索引分为几列 - Split Pandas dataframe index into columns according to specific rule 如何选择与数据框中的一个特定列高度相关的列 - How to select columns that are highly correlated with one specific column in a dataframe Pandas dataframe,如何按多列分组并为特定列应用总和并添加新的计数列? - Pandas dataframe, how can I group by multiple columns and apply sum for specific column and add new count column? 将 dataframe 的多个列的值复制到另一个 dataframe 的 1 列中 - Copy the values of multiple columns of a dataframe into 1 column of another dataframe 如何在展平 MultiIndex 列并将样式应用于 DataFrame 后删除索引列? - How to remove the index column after flattening MultiIndex columns and apply styling to the DataFrame? 熊猫数据框:如何根据行中的值复制其他列 - panda dataframe: how to copy some columns in others according to a value in the row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM