我有一堆CSV文件.在其中一些中,缺失的数据由空单元格表示,但在其他情况下则有一段时间.我想循环遍历所有文件,打开它们,删除任何单独出现的时间段,然后保存并关闭文件.
我已经阅读了一些关于使用re.sub()进行全字搜索的其他问题.这就是我想做的事情(删除.当它唯独不见的发生.3.5),但我不能得到正确的语法为整词,只搜索在整个字是一个特殊字符(". ").另外,我担心这些答案可能会有一些不同,在这种情况下,整个单词也可以通过制表符和换行符来区分.也就是说,/ b在我的CSV文件中工作吗?
更新:这是一个功能,我看到下面的帮助后结束了写作.也许它会对其他人有用.
import csv, re
def clean(infile, outfile, chars):
'''
Open a file, remove all specified special characters used to represent missing data, and save.\n\n
infile:\tAn input file path\n
outfile:\tAn output file path\n
chars:\tA list of strings representing missing values to get rid of
'''
in_temp = open(infile)
out_temp = open(outfile, 'wb')
csvin = csv.reader(in_temp)
csvout = csv.writer(out_temp)
for row in csvin:
row = re.split('\t', row[0])
for colno, col in enumerate(row):
for char in chars:
if col.strip() …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用把手来格式化从JSON文件接收的数据.我目前的结构类似于:
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Mark</th>
<th>Subject</th>
<th>School</th>
<th>Birthday</th>
</tr>
</thead>
<tbody>
{{#each students}}
<tr>
<td>{{ this.name }}</td>
<td>{{ this.mark }}</td>
<td>{{ this.subject }}</td>
<td>{{ this.school }}</td>
<td>{{ this.birthday }}</td>
</tr>
{{/each}}
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我拥有的JSON文件没有一致的结构,一些学生元素只包含名称(缺少所有其他字段),有些只包含名称和学校.
如果我使用我当前的代码来模拟JSON文件,我将获得一个包含大量空白单元格的表,我想用"null"来编写.
我想也许我应该写一个寄存器,但如果是这样,我该怎么办?
我不能在lme4(1.1-7)的模型中插入随机斜率:
> difJS<-lmer(JS~Tempo+(Tempo|id),dat,na.action=na.omit)
Error: number of observations (=274) <= number of random effects (=278) for term
(Tempo | id); the random-effects parameters and the residual variance (or scale
parameter) are probably unidentifiable
Run Code Online (Sandbox Code Playgroud)
使用nlme它正在工作:
> JSprova<-lme(JS~Tempo,random=~1+Tempo|id,data=dat,na.action=na.omit)
> summary(JSprova)
Linear mixed-effects model fit by REML Data: dat
AIC BIC logLik
769.6847 791.3196 -378.8424
Random effects:
Formula: ~1 + Tempo | id
Structure: General positive-definite, Log-Cholesky parametrization
StdDev Corr
(Intercept) 1.1981593 (Intr)
Tempo 0.5409468 -0.692
Residual 0.5597984
Fixed effects: JS ~ Tempo
Value …Run Code Online (Sandbox Code Playgroud) 我对R比较新,遇到了一些问题.我正在使用某些年份缺少值的数据框.例如:
year var1 var2
1972 1.3 1.4
1973 1.6 2.8
1974 2.0 1.5
1975 NA NA
1976 1.5 2.1
1977 NA NA
1978 1.9 1.1
Run Code Online (Sandbox Code Playgroud)
对于每个NA,我想取上一行和下一行的平均值.因此1975年的var1和var2应分别为1.75和1.8.1977年他们应该是1.7和1.6.有任何想法吗?
如果我有一个向量并且我有一个起始编号,那么std :: algorithm是否为我提供了找到下一个最高缺失数字的方法?
我显然可以在一个嵌套的循环中写这个,我只是不能动摇我重新发明轮子的感觉.
例如,给定: vector foo{13,8,3,6,10,1,7,0};
起始编号0应该找到2.
起始编号6应该找到9.
起始编号-2应该找到-1.
编辑:
迄今为止,所有解决方案都需要分类 实际上这可能是必需的,但是vector必须创建一个临时排序以适应这一点,因为foo必须保持不变.
我有500个数据集(面板数据).每个我都有不同商店(商店)的时间序列(周).在每个商店内,我需要添加缺少的时间序列观察.
我的数据样本将是:
store week value
1 1 50
1 3 52
1 4 10
2 1 4
2 4 84
2 5 2
Run Code Online (Sandbox Code Playgroud)
我想看起来像:
store week value
1 1 50
1 2 0
1 3 52
1 4 10
2 1 4
2 2 0
2 3 0
2 4 84
2 5 2
Run Code Online (Sandbox Code Playgroud)
我目前使用以下代码(它可以工作,但我的数据需要很长时间):
stores<-unique(mydata$store)
for (i in 1:length(stores)){
mydata <- merge(
expand.grid(week=min(mydata$week):max(mydata$week)),
mydata, all=TRUE)
mydata[is.na(mydata)] <- 0
}
Run Code Online (Sandbox Code Playgroud)
是否有更好,更有效的方法?
如果我有:
s <- data.frame(ID=c(191, 282, 202, 210), Group=c("", "A", "", "B"), stringsAsFactors=FALSE)
s
ID Group
1 191
2 282 A
3 202
4 210 B
Run Code Online (Sandbox Code Playgroud)
我可以用N替换空单元格,如下所示:
ds$Group[ds$Group==""]<-"N"
s
ID Group
1 191 N
2 282 A
3 202 N
4 210 B
Run Code Online (Sandbox Code Playgroud)
但是我需要用另一列中的值替换空单元格。我该怎么做?:
s
ID Group Group2
1 191 D D
2 282 A G
3 202 G G
4 210 B D
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,有两列,我正在分组dplyr,一列数月(如数字,例如1到12),以及几个列后面有统计数据(值不重要).一个例子:
ID_1 ID_2 month st1 st2
1 1 1 0.5 0.2
1 1 2 0.7 0.9
1 1 3 1.1 1.7
1 1 4 2.6 0.8
1 1 5 1.8 1.3
1 1 6 2.1 2.2
1 1 7 0.5 0.2
1 1 8 0.7 0.9
1 1 9 1.1 1.7
1 1 10 2.6 0.8
1 1 11 1.8 1.3
1 1 12 2.1 2.2
1 2 1 0.5 0.2
1 2 2 0.7 0.9
1 2 …Run Code Online (Sandbox Code Playgroud) 我收到来自多个天线的每日txt数据文件.文件的命名约定是:
独特的天线ID +年+月+日+随机3位数
我解析了文件名并创建了一个像这样的表:
AntennaID fileyear filemonth fileday filenumber filename
0000 2016 09 22 459 000020160922459.txt
0000 2016 09 21 981 000020160921981.txt
0000 2016 09 20 762 000020160920762.txt
0001 2016 09 22 635 000120160922635.txt
.
.
.
etc. (200k rows)
Run Code Online (Sandbox Code Playgroud)
有时天线发送的文件多于1个或根本没有文件.如果发送的文件超过1,则唯一的3位数文件编号会区分文件,但我正在尝试查找未发送文件的日期.
我已经尝试了几个groupby语句来比较给定月份的数据文件数量,看看它是否与那个月的天数相匹配 - 但问题是有时候天线每天发送超过1个文件,这可能是人为的如果我们只是比较计数,则弥补"遗失"文件.
我正在寻找一种更健壮的方法来查找丢失文件的日期或日期范围.我已经查看了Partition和Over函数,感觉可能存在潜力,但我不确定如何使用它们,因为我对SQL很新.
我正在使用Microsoft SQL Server 2016
sql sql-server missing-data gaps-and-islands sql-server-2016
我想对熊猫数据框中的几列数据进行解冻。每列都有一些NaN,这会影响winsorization,因此需要将其删除。我知道如何执行此操作的唯一方法是针对所有数据删除它们,而不是仅逐列删除它们。
MWE:
import numpy as np
import pandas as pd
from scipy.stats.mstats import winsorize
# Create Dataframe
N, M, P = 10**5, 4, 10**2
dates = pd.date_range('2001-01-01', periods=N//P, freq='D').repeat(P)
df = pd.DataFrame(np.random.random((N, M))
, index=dates)
df.index.names = ['DATE']
df.columns = ['one','two','three','four']
# Now scale them differently so you can see the winsorization
df['four'] = df['four']*(10**5)
df['three'] = df['three']*(10**2)
df['two'] = df['two']*(10**-1)
df['one'] = df['one']*(10**-4)
# Create NaN
df.loc[df.index.get_level_values(0).year == 2002,'three'] = np.nan
df.loc[df.index.get_level_values(0).month == 2,'two'] = np.nan
df.loc[df.index.get_level_values(0).month …Run Code Online (Sandbox Code Playgroud) missing-data ×10
r ×5
python ×2
algorithm ×1
c++ ×1
counting ×1
csv ×1
dplyr ×1
html-table ×1
javascript ×1
json ×1
lme4 ×1
mean ×1
nan ×1
pandas ×1
replace ×1
scipy ×1
sql ×1
sql-server ×1
text ×1
time-series ×1
vector ×1