你好,
我想删除包含“apple”的所有列,并删除具有特定值-9999的所有行(无论列)。 我已经启动了删除列的代码,但我没有得到它。
谢谢。
dt = Open("file.csv");
//dt = Open( "$SAMPLE_DATA/Cities.jmp" );
col_list = dt << get column names( string );
For( i = N Items( col_list ), i > 0, i--,
If(Contains( col_list[i], "apple" ),
Remove From( col_list, i ),
)
);
//dt << Select Where( );
//dt << Delete Rows;
dt << save( "testOUT.txt" );这是对脚本的非常简单的更改。可以消除用于输入数据的所有特殊选项。Open() 函数将仅使用默认值。
其次,当确定输入列本质上是字符时,对行删除要做的就是搜索字符串值“-9999”。
名称默认为 Here(1);
dt=打开(
“文件(3).csv”);
// 创建一个列表来放置要删除的列
删除列表 = {};
For( i = 1, i<= N Cols(dt), i++,
If( Contains( Uppercase( as Column( i ) << 获取名称 ), "APPLE" ) > 0,
插入(delList,列(i)<<获取名称)
)
);
dt << 删除列( delList);
// 循环遍历数据并删除所有包含的行
// 任意列中的值 -9999
For( i = 1, i <= N Cols( dt ), i++,
If( Column( dt, i ) << 获取数据类型 == "数值",
dt << 选择其中( As Column( dt, i ) == -9999 ),
dt << 选择其中(包含(作为列(dt,i),“-9999”)== 1 |
包含(作为列(dt,i),“99”)== 1
)
);
If( N 行( dt << 获取所选行 ) > 0,
dt << 删除行
);
);
吉姆,
该代码有效。
当我使用“|”时管道。 它将创建额外的列。
在这种情况下,我不需要任何额外的列标题,总共将是“12”列。
但在每一列中,我可以有几个管道数据。 我只关心第一个值而忽略其余的。
例子:
“99|23|abc|x”,我只关心“99”,忽略其余的。
“-9999|123”,我只关心“-9999”,该行稍后将被删除。
1 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 -9999|999|231 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 1|xyz|abc 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 -9999 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 99|23|abc|x 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
-9999|123 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1 1 1 2 30 40 1 10 99
1 1 1 1|87 1 -9999 2 30 40 1 10 99
这是一个可以完成您想要的操作的脚本。关于脚本的几点:
1. Open()函数有“|”除了“,”之外还添加作为分隔符。这是通过打开 .csv 文件时使用“数据预览”轻松创建的。它允许选择附加字符用作分隔符。然后需要做的就是打开文件,然后转到表面板中的源条目,对其进行编辑并复制出 JMP 创建的用于打开文件的源代码。
2. 包含 Apple 的列的删除必须进行概括,因为在一个列中它被拼写为 Apple,而在另一列中它被拼写为 apple。
3. 我真的不认为你应该根据发现-9999 的出现来删除整行。我真的认为你应该将值 -9999 设置为缺失值。如果您愿意,我提供了一段可以以这种方式处理您的数据的代码。
names default to Here(1);
dt=Open(
"file.csv",
columns(
New Column( "a", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "b", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "c", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "d", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "e", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "g", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "apple", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "orange", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "candy", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "pizza", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "Apple2_2", Numeric, "Continuous", Format( "Best", 12 ) ),
New Column( "orange2", Numeric, "Continuous", Format( "Best", 12 ) )
),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Comma, Other( "|" ), CSV( 0 ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 1 ),
Column Names Start( 1 ),
Data Starts( 2 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
// Create a list to place columns to be deleted into
delList = {};
For( i = 1, i<= N Cols(dt), i++,
If( Contains( Uppercase( as Column( i ) << get name ), "APPLE" ) > 0,
insert into(delList, column(i)<<GET name=""> 0,
dt << delete rows
);
)
);
/*// Substitute the code below for the last For Loop in the above
// script, if what you want to do is to just change the -9999
// values to missing values
For( i = 1, i <= N Cols( dt ), i++,
If( Column( dt, i ) << get datatype == "Numeric",
dt << select where( As Column( dt, i ) == -9999 );
If( N Rows(dt << get selected rows) > 0,
column(dt,i)[dt << get selected rows] = .;
);
)
);*/</GET>