使用python pandas查找剩余天数的平均值

时间:2018-11-21 19:49:42

标签: python pandas dataframe percentage

df是一个csv文件,其中包含ship_date,order_date和cumulative_ordered。 cumulative_ordered是直到ship_date为止每天增加的订单总数。每个ship_date之前有30天,而这些天只算作一个ship_date。在ship_date 2018-07-01之后,然后下一个ship_date将是2018-08-01,采用相同的程序。

我的问题是,当我计算前30天中的每一天的cumulative_ordered百分比平均值时,我没有剩余的天数(请参阅下面的最后代码输出)。

我有以下代码,它为我提供了一个csv文件中的days_remaining,其中有几个不同的ship_date和order_date递减到每个单独的ship_date。

df['days_remaining'] = pd.to_datetime(df['ship_date']).sub\
(pd.to_datetime(df['order_date'])).dt.days
df['difference'] = df['ship_date'] - df['order_date']

df.head()

输出:

ship_date    Order_date   cumulative_ordered   days_remaining    difference

2018-07-01   2018-06-01     7                  30               30 days
2018-07-01   2018-06-02     10                 29               29 days
2018-07-01   2018-06-03     15                 28               28 days
2018-07-01   2018-06-04     30                 28               27 days
2018-07-01   2018-06-05     41                 28               26 days

然后我尝试查找ship_date之前每天订购的总数

m = df.groupby("difference").mean()
m.head()

这给了我这个输出:

             cumulative ordered    days_remaining
difference                            
      0 days    352.458124             0.0
      1 days    291.234747             1.0
      2 days    244.122137             2.0
      3 days    201.178765             3.0
      4 days    190.153641             4.0

当我尝试通过运行以下代码来尝试计算从每天0天以上的cumulative_ordered输出中填充的百分比得出的每天累积订购的平均值时,遇到了一个问题:

   v = m/m[m.index.days == 0].iloc[0]
   v.head()

          cumulative_ordered      days_remaining
difference                           
0 days        1.000000              NaN
1 days        0.891324              inf
2 days        0.812534              inf
3 days        0.752339              inf
4 days        0.673745              inf

days_remaining更改为NaN和inf。如何保留它,以便它仍为我提供整数?

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <div style="font-size: 25pt; text-align: center">Pig Latin Translator</div> <br /><br /> <div style="font-size: 15pt; text-align: center"> Enter text to be translated: </br> <textarea id="sentence" rows="5" cols="30"></textarea><br /> <button onclick="Translate()">Submit</button> </div> <div id="output"></div> <script type="text/javascript"> function Translate() { var sentence = document.getElementById("sentence").value; var wordStart = -1; var wordEnd = -1; var letter; var fullSentence; for (let i = 0; i < sentence.length; i++) { var trans; var c = sentence.charAt(i).toLowerCase(); if (wordStart === -1 && (c !== " " || c !== "." || c !== "," || c !== "!" || c !== "?")) { wordStart = i; letter = c; } if ( wordEnd === -1 && (c === " " || c === "." || c === "," || c === "!" || c === "?" || i === sentence.length - 1) ) { wordEnd = i; } if ( wordStart !== -1 && wordEnd !== -1 && (letter !== "a" || letter !== "e" || letter !== "i" || letter !== "o" || letter !== "u") ) { trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay"; wordStart = -1; wordEnd = -1; } if ( wordStart !== -1 && wordEnd !== -1 && (letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u") ) { trans = sentence.substring(wordStart, wordEnd) + "way"; wordStart = -1; wordEnd = -1; } fullSentence = fullSentence + trans + " "; } document.getElementById("output").innerHTML = fullSentence; } </script> </body> </html>NaN除以inf所得。

似乎您仅尝试将操作应用于0.0列,因此您应该在最后的代码块中运行此操作:

cumulative_ordered