我有一个如下所示的数据集:
Id City Color City_1 Color_1
123 Miami Nan Miami Nan
124 Miami nan Nan Miami
125 Seattle Nan Mexico Nan
126 Nan white Nan Yellow
127 Wash Nan Wash Nan
128 LA pink
我正在编写以下代码:
# Resample, interpolate and inspect ozone data here
data = data.resample('D').interpolate()
data.info()
# Create the rolling window
***rolling = data.rolling(360)['Ozone']
# Insert the rolling quantiles to the monthly returns
data['q10'] = rolling.quantile(.1)
dat
熊猫的DataFrame.interpolate()与它所考虑的行数之间是如何工作的:
是不是只有南军之前的那一排和后面的那一排?
或者是整个DataFrame (在100万行时如何工作?)
或者另一种方式(请解释)
每种方法都是相关的。
‘linear’: Ignore the index and treat the values as equally spaced. This is the only method supported on MultiIndexes.
‘time’: Works on daily an
如下所示:
Time Lat Long
19:24:52.135 35.61067 139.630228
19:24:52.183 NaN NaN
19:24:52.281 NaN NaN
19:24:52.378 NaN NaN
19:24:52.466 35.610692 139.630428
需要为Lat和Long字段填写NaN值,以便每一行具有Lat / Long的NaN值时都取值,以便:
它们在下一个(比方说x2,y2)和
以前的非NaN lat/long (例如x1,y1)点和它们之间的间隔相等。
在上述情况下,由
试图替换我的数据框架中列为'nan‘的值(注意,不是'NaN')
我在excel文件中读过,然后尝试替换nan值,如下所示:
All_items_df = ALL_df[df_items].fillna(' ')
最后,我得到了一个仍然包含'nan‘的输出
All_items_df ['Colour'].head(10)
Out[]:
7 nan
8 nan
9 nan
10 nan
13 nan
14 nan
15 nan
16 nan
18 nan
19 na
在一个环境中,我的pandas版本为0.17.0,numpy版本为1.10.1。在另一个环境中,我的pandas版本为0.18.1,numpy版本为1.10.4。
我运行这段代码
from pandas import Series
import numpy as np
Series([1,2,3,4,5,np.NaN]).describe()
对于pandas版本0.17.0,我得到以下输出:
count 5.000000
mean 3.000000
std 1.581139
min 1.000000
25% 2.000000
50% 3
我希望生成一个列count,该列按id计算pts组的值。条件是,如果x和y都包含NaN,则相应的pts将被计数,否则将被忽略。
样本Df:
id pts x y
0 1 0.1 NaN NaN
1 1 0.2 1.0 NaN
2 1 1.1 NaN NaN
3 2 0.1 NaN NaN
4 2 0.2 2.0 1.0
5 3 1.1 NaN NaN
6 3 1.2 NaN 5.0
7 3 3.1 NaN NaN
8 3 3.2
假设我有一个数据文件,就像:
1. A B
2. a1 b1
3. a2 NaN
4. a3 NaN
我如何用例如(b1/a1)*a2和(b1/a1)*a3来填充NaNs
我想像df.apply(pd.Series.my_function)这样的东西是必须使用的。有人能帮我解决这个问题吗?
编辑:我的陈述一定是误导性的。NaN可以在数据中的任何地方到达,我想用(最近的非NaN B/最近的非NaN A)*a2来填充NaNs。
我有一个从-1到+1的Pandas系列随机数:
from pandas import Series
from random import random
x = Series([random() * 2 - 1. for i in range(1000)])
x
Output:
0 -0.499376
1 -0.386884
2 0.180656
3 0.014022
4 0.409052
...
995 -0.395711
996 -0.844389
997 -0.508483
998 -0.156028
目标是通过id生成列id组,其中'pct' = (1st value of 'pts' group by 'id' * 100) / number of same consecutive 'id' value where 'x' and 'y' both are 'NaN'。例如,当id=1,pct = (5*100) / 2 = 250。它会循环整个数据。
样本df:
id pts x y
0 1 5 NaN NaN
1 1 5