你好,
我想刪除包含“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>