简体   繁体   English

用正数创建数据框行,用负数创建其他行

[英]Create dataframe row with positive numbers and other with negative

I have the following dataframe called Utilidad 我有以下称为Utilidad的数据

Argentina Bolivia   Chile   España  Uruguay
2004       3     6       1        3       2
2005       5     1       4        1       5

And I calculate the difference between 2004 and 2005 using 我使用以下方法计算2004年和2005年之间的差异

Utilidad.ix['resta']=Utilidad.ix[2005]-Utilidad.ix[2004]

Now I'm trying to create two additional rows, one with the result of the difference when is positive and the other one with the negatives, something like this 现在,我尝试创建另外两行,其中一行的结果是当正数为差时,另一行的结果为负数,如下所示

Argentina Bolivia   Chile   España  Uruguay
2004       3     6       1        3       2
2005       5     1       4        1       5
resta      2    -5       3       -2       3
positive   2     0       3        0       3
negative   0    -5       0       -2       0

The only I have managed to do is to have an additional column which tells me wheter "resta" is positive or not, using 我唯一能做的就是增加一栏,告诉我“ resta”是否为正,使用

Utilidad.ix['boleano'][Utilidad.ix['resta']>0]

Can someone help me to create this two additional rows? 有人可以帮我创建另外两行吗?

Thanks 谢谢

You can use numpy.where 您可以使用numpy.where

df.ix['positive'] = np.where(df.ix['resta'] > 0, df.ix['resta'], 0)
df.ix['negative'] = np.where(df.ix['resta'] < 0, df.ix['resta'], 0)

numpy.clip will be handy here, or just calculate it . numpy.clip在这里会很方便,或者只计算它即可。

In [35]:

Utilidad.ix['positive']=np.clip(Utilidad.ix['resta'], 0, np.inf)
Utilidad.ix['negative']=np.clip(Utilidad.ix['resta'], -np.inf, 0)
#or
Utilidad.ix['positive']=(Utilidad.ix['resta']+Utilidad.ix['resta'].abs())/2
Utilidad.ix['negative']=(Utilidad.ix['resta']-Utilidad.ix['resta'].abs())/2
print Utilidad
          Argentina  Bolivia  Chile  España  Uruguay
id                                                  
2004              3        6      1       3        2
2005              5        1      4       1        5
resta             2       -5      3      -2        3
positive          2        0      3       0        3
negative          0       -5      0      -2        0

[5 rows x 5 columns]

Some speed comparisons: 一些速度比较:

%timeit (Utilidad.ix['resta']-Utilidad.ix['resta'].abs())/2
1000 loops, best of 3: 627 µs per loop
In [36]:

%timeit Utilidad.ix['positive'] = np.where(Utilidad.ix['resta'] > 0, Utilidad.ix['resta'], 0)
1000 loops, best of 3: 647 µs per loop
In [38]:

%timeit Utilidad.ix['positive']=np.clip(Utilidad.ix['resta'], 0, 100)
100 loops, best of 3: 2.6 ms per loop
In [45]:

%timeit Utilidad.ix['resta'].clip_upper(0)
1000 loops, best of 3: 1.32 ms per loop

The observation to make here is that negative is the minimum of 0 and the row: 这里要观察的是负数是0和行的最小值:

In [11]: np.minimum(df.loc['resta'], 0)  # negative
Out[11]:
Argentina    0
Bolivia     -5
Chile        0
España      -2
Uruguay      0
Name: resta, dtype: int64

In [12]: np.maximum(df.loc['resta'], 0)  # positive
Out[12]:
Argentina    2
Bolivia      0
Chile        3
España       0
Uruguay      3
Name: resta, dtype: int64

Note: If you are concerned about speed then it would make sense to transpose the DataFrame, since appending columns is much cheaper than appending rows. 注意:如果您担心速度,那么转置DataFrame是有意义的,因为附加列比附加行便宜得多。

You can append a row using loc: 您可以使用loc附加一行:

df.loc['negative'] = np.minimum(df.loc['resta'], 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据框并列出所有正行和负行 - dataframe and list all the positive row and negative row 从 dataframe 中删除连续的正数/负数 - Remove consecutive positive/negative numbers from a dataframe 将 Pandas Dataframe 的列(包含负数和正数)除以特定数字,并在 python 中创建一个新列? - Divide a column(containing both negative numbers and positive numbers) of a Pandas Dataframe with a specific number and create a new column in python? 在有正数和负数的 dataframe 中,如何将正数向上舍入和将负数向下舍入(均为 5 的倍数)? - In a dataframe with both positive & negative numbers, how to round positive numbers up and round negative numbers down (both to multiples of 5)? 如何从 dataframe 的列表中同时删除正数和负数? - How to remove both positive and negative numbers from a list in dataframe? 如何创建仅具有正数的随机数据框? - How to create random dataframe with positive numbers only? 如何根据出现的行总结列表中的负数和正数 - How to sum up negative and positive numbers in a list based on the row they occur 创建一个新的 Dataframe 计算每个用户的正面和负面推文 - Create a new Dataframe that counts positive and negative tweets for each user 负数和正数之间的按位 AND (&amp;)? - Bitwise AND (&) between negative and positive numbers? 测量数据帧的负/正偏度 - Measure negative/positive skewness of a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM