[英]how do you divide each value from a pandas series in sequence
Hi I was trying to figure out how to divide values from a DataFrame.嗨,我试图弄清楚如何从 DataFrame 中划分值。 But here I made an example for pandas series但是这里我为 pandas 系列做了一个例子
a = pd.Series([1, 2, 16,64,128,360,720])
a
-----------------
0 1
1 2
2 16
3 64
4 128
5 360
6 720
So is there any way I could divide a number in a given row by the value from the previous row?那么有什么方法可以将给定行中的数字除以前一行的值?
0 2
1 8
2 4
3 2
4 2.8
5 2
Furthermore, I also tried to get the output like "if the value is double, print the index".此外,我还尝试获取 output ,例如“如果值为双倍,则打印索引”。
Thank you for your help!谢谢您的帮助!
Well I don't know what exactly you want to divide your pandas Series by, but you can do it like this:好吧,我不知道您到底想将 pandas 系列除以什么,但您可以这样做:
# If you want to store a new result
b = a / value_to_divive_by
# or if you want to apply it directly to your serie
a /= value_to_divive_by
or using list comprehension或使用列表理解
b = [int(nb / your_value_here) for nb in a]
# with a min value to do the divison
b = [int(nb / your_value_here) for nb in a if nb > min_value]
There is probably other ways to do what you want, but there is two easy solutions可能有其他方法可以做你想做的事,但有两个简单的解决方案
What it seems to me is that you are trying to divide a number in a given row by the one of the previous.在我看来,您正试图将给定行中的数字除以前一个数字。 This can be achieved using this code这可以使用此代码来实现
import pandas as pd
import numpy as np
a = pd.Series([1, 2, 16,64,128,360,720])
division = pd.Series(np.divide(a.values[1:],a.values[:-1]))
index = pd.Series(np.multiply(division == 2, [i for i in range(len(a)-1)]))
Note: your question is very ill posed.注意:你的问题很不恰当。 You didn't specify what you wanted to achieve, I figured out by myself from the example.您没有指定要实现的目标,我自己从示例中弄清楚了。 You also added a wrong snipped of code.您还添加了错误的代码片段。 Pay attention to make a nicer question next time下次注意提出更好的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.