我有一个pandas数据帧,我插入它来获取每日数据帧.原始数据框如下所示:
col_1 vals
2017-10-01 0.000000 0.112869
2017-10-02 0.017143 0.112869
2017-10-12 0.003750 0.117274
2017-10-14 0.000000 0.161556
2017-10-17 0.000000 0.116264
Run Code Online (Sandbox Code Playgroud)
在插值数据帧中,我想将数据值更改为NaN,其中日期差距超过5天.例如在上面的数据帧之间的间隙2017-10-02和2017-10-12超过因此5天内插数据帧这2个日期之间的所有值应被删除.我不知道怎么做,也许combine_first吧?
--EDIT:插值数据帧如下所示:
col_1 vals
2017-10-01 0.000000 0.112869
2017-10-02 0.017143 0.112869
2017-10-03 0.015804 0.113309
2017-10-04 0.014464 0.113750
2017-10-05 0.013125 0.114190
2017-10-06 0.011786 0.114631
2017-10-07 0.010446 0.115071
2017-10-08 0.009107 0.115512
2017-10-09 0.007768 0.115953
2017-10-10 0.006429 0.116393
2017-10-11 0.005089 0.116834
2017-10-12 0.003750 0.117274
2017-10-13 0.001875 0.139415
2017-10-14 0.000000 0.161556
2017-10-15 0.000000 0.146459
2017-10-16 0.000000 0.131361
2017-10-17 0.000000 0.116264
Run Code Online (Sandbox Code Playgroud)
预期产量: …
我想基于python中的以下字典替换2D numpy数组中的值:
code region
334 0
4 22
8 31
12 16
16 17
24 27
28 18
32 21
36 1
Run Code Online (Sandbox Code Playgroud)
我想在numpy2D数组中找到匹配code并替换为region列中相应值的单元格。问题在于,这将导致替换code = 12为region = 16和在下一行中,所有值为16的单元格(包括刚刚被赋值为16的单元格)都将被替换为值17。如何防止这种情况?
我有 2 个 numpy 数组:
arr_a = array(['1m_nd', '2m_nd', '1m_4wk'],
dtype='<U15')
arr_b = array([0, 1, 1])
Run Code Online (Sandbox Code Playgroud)
我想从arr_a基于arr_b. 我正在这样做:
arr_a[arr_b],但我得到了这个结果:
array(['1m_nd', '2m_nd', '2m_nd'],
dtype='<U15')
Run Code Online (Sandbox Code Playgroud)
代替:
array(['2m_nd', '1m_4wk'],
dtype='<U15')
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
rcParams['date.autoformatter.month'] = "%b\n%Y"
Run Code Online (Sandbox Code Playgroud)
我正在使用 matpltolib 绘制时间序列,如果我按照上面的方式设置 rcParams,则生成的图会在每个刻度处标记月份名称和年份。我如何设置它以便仅在每年的 1 月绘制该年份。我尝试这样做,但它不起作用:
rcParams['date.autoformatter.month'] = "%b"
rcParams['date.autoformatter.year'] = "%Y"
Run Code Online (Sandbox Code Playgroud) with NamedTemporaryFile(suffix='.shp').name as tmp_shp:
df.to_file(tmp_shp)
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我收到此错误:
with NamedTemporaryFile(suffix='.shp').name as tmp_shp:
df.to_file(tmp_shp)
Run Code Online (Sandbox Code Playgroud)
如何使用 with 语句使用命名临时文件?既然tmp_shp只是一条路径,它在 之外仍然可用吗with?
我使用 xarray concat 命令创建了一个 xarray 数据数组。这导致了一个名为 name 的数据数组__xarray_dataarray_variable__。但是,我似乎无法使用rename命令重命名它。还有其他方法可以重命名吗?
我试过这个:
\n\nda.rename({\'__xarray_dataarray_variable__\': \'new\'})\nRun Code Online (Sandbox Code Playgroud)\n\n但我收到此错误:*** ValueError: cannot rename \'__xarray_dataarray_variable__\' because it is not a variable or dimension in this dataset
数据数组如下所示:
\n\n<xarray.DataArray (time: 2, band: 1, y: 2334, x: 4258)>\ndask.array<shape=(2, 1, 2334, 4258), dtype=float32, chunksize=(1, 1, 2334, 4258)>\nCoordinates:\n * band (band) int32 1\n * y (y) float64 4.406e+06 4.406e+06 4.406e+06 4.406e+06 4.406e+06 ...\n * x (x) float64 1.125e+05 1.126e+05 1.127e+05 1.128e+05 1.129e+05 ...\n …Run Code Online (Sandbox Code Playgroud) 这个问题的推论:当另一列中存在 N 个 NaN 时,替换 Pandas 列中的值
a b c d e
2018-05-25 0.000381 0.264318 land 2018-05-25
2018-05-26 0.000000 0.264447 land 2018-05-26
2018-05-27 0.000000 0.264791 NaN NaT
2018-05-28 0.000000 0.265253 NaN NaT
2018-05-29 0.000000 0.265720 NaN NaT
2018-05-30 0.000000 0.266066 land 2018-05-30
2018-05-31 0.000000 0.266150 NaN NaT
2018-06-01 0.000000 0.265816 NaN NaT
2018-06-02 0.000000 0.264892 land 2018-06-02
2018-06-03 0.000000 0.263191 NaN NaT
2018-06-04 0.000000 0.260508 land 2018-06-04
2018-06-05 0.000000 0.256619 NaN NaT
2018-06-06 0.000000 0.251286 NaN NaT
2018-06-07 0.000000 …Run Code Online (Sandbox Code Playgroud) 我希望val在以下数据框中提前3天将列中的所有值移动:
datetime val val_b
12/20/2010 23
12/21/2010 12
12/22/2010 23 27
12/23/2010 26
12/24/2010 28
12/25/2010 17
12/26/2010 26
12/27/2010 21 14
12/28/2010 20
12/29/2010 18
12/30/2010 15 22
12/31/2010 20
1/1/2011 13
1/2/2011 12 30
1/3/2011 25
1/4/2011 15
1/5/2011 19
1/6/2011 14
Run Code Online (Sandbox Code Playgroud)
我尝试使用该pd.DateOffset功能,但是帽子会向前移动所有列,我不希望这样.
我有一个2列数组,第1列权重和第2列值,我正在使用python绘制.我想从这个加权数组中抽取20个样本,与它们的权重成比例.有没有python/numpy命令这样做?
我有一个数据框,我执行一些操作并打印出来.要做到这一点,我必须遍历每一行.
for count, row in final_df.iterrows():
x = row['param_a']
y = row['param_b']
# Perform operation
# Write to output file
Run Code Online (Sandbox Code Playgroud)
我决定使用python多处理模块并行化这个
def write_site_files(row):
x = row['param_a']
y = row['param_b']
# Perform operation
# Write to output file
pkg_num = 0
total_runs = final_df.shape[0] # Total number of rows in final_df
threads = []
import multiprocessing
while pkg_num < total_runs or len(threads):
if(len(threads) < num_proc and pkg_num < total_runs):
print pkg_num, total_runs
t = multiprocessing.Process(target=write_site_files,args=[final_df.iloc[pkg_num],pkg_num])
pkg_num = pkg_num + 1
t.start() …Run Code Online (Sandbox Code Playgroud)