열 순서가 다른 여러 테이블 연결팀,
각각 200열을 갖는 5개의 csv 파일이 있습니다(각 파일의 열 수가 동일하지는 않습니다). 각 파일에는 서로 다른 순서의 열이 있습니다. 사용자가 5개 파일을 모두 선택한 다음 5개 파일을 모두 결합하고(일치하지 않는 열 삭제) 결합된 csv 파일을 필요한 위치에 저장할 수 있는 스크립트를 작성하고 싶습니다. (data = Open(Pick File("Choose the File", x)), x는 디렉터리)와 유사한 명령을 사용하여 파일을 선택하고 싶습니다.
감사해요
Re: 열 순서가 다른 여러 테이블을 연결합니다.감사해요 @txnelson
당신은 항상 큰 도움이 됩니다.Re: 열 순서가 다른 여러 테이블을 연결합니다.여기에는 한 번에 연결할 모든 파일을 선택한 다음 작은 스크립트를 실행하여 일치하는 열만 유지하면서 스크립트를 재작업한 것입니다. 귀하의 사양에 맞게 만들었지만 원하는 수의 파일을 연결할 수 있도록 쉽게 변경할 수 있습니다.
Names Default To Here( 1 );
inputDirectory = "C:\";
// Select the files to be read in......this can be any number of files
filePath = Pick File(
"Choose the File to be Concatenated to:",
inputDirectory,
{"CSV Files|csv;", "All Files|*"},
1,
0,
""
);
// Open the first file
dtMain = Open( filePath, invisible );
files = Pick File(
"Choose the Additional 4 Files to Read In:",
inputDirectory,
{"CSV Files|csv;", "All Files|*"},
1,
0,
"",
multiple
);
// Validate that files have been read in
If( Try( N Items( files ), 0 ) == 4,
Dialog( "Incorrect number of files were read in."
"Only 4 additional files can be read in");
Throw( "Incorrect number of files were read in" );
);
// Read in the remaining files and process them
For( i = 2, i <= N Items( Files ), i++,
Try( dtConCat = Open( Files[i], invisible ) );
mainColList = dtMain << Get Column Names( string );
conCatColList = dtConCat << Get Column Names( string );
// Delete the columns in the dtConCat table that do not exist in the dtMain table
delList = {};
For( k = 1, k <= N Items( conCatColList ), k++,
If( N Rows( Loc( mainColList, conCatColList[k] ) ) == 0,
Insert Into( delList, conCatColList[k] )
)
);
If( N Items( delList ) > 0,
dtConCat << delete columns( delList )
);
// Delete the columns in the dtMain table that were not found in the dtConCat table
delList = {};
For( k = 1, k <= N Items( mainColList ),
k++,
If( N Rows( Loc( conCatColList, mainColList[k] ) ) == 0,
Insert Into( delList, mainColList[k] )
)
);
If( N Items( delList ) > 0,
dtMain << delete columns( delList )
);
// If any columns still remain in the two tables, then concatenate them together
If( N Cols( dtMain ) > 0 & N Cols( dtConCat ) > 0,
dtMain = dtMain << Concatenate( dtConCat, Append to First Table )
);
// Close the no longer needed file
Close( dtConCat, nosave );
);
// Make the main file visible
dtMain << show window( 1 );
Re: 열 순서가 다른 여러 테이블을 연결합니다.@라자트 ,
왜 새 테이블을 만들어야 하는지 이해할 수 없습니다. 나는 이런 일을 할 것입니다.
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
OriginalColOrder = dt << Get Column Names("string");
dt << Reorder By Name();
SortedColOrder = dt << Get Column Names("string");
Show(OriginalColOrder);
Show(SortedColOrder); 목록은 정렬된 순서와 원래 순서를 모두 제공합니다. 또한, 지금까지 시도한 내용을 공유해 주시면 어디로 향하고 있는지 더 쉽게 알 수 있습니다. 나는 당신이 지금까지 무엇을 시도했고 어디에서 실패했는지 정확히 이해할 수 없습니다. 데이터를 공유할 수 없는 경우 샘플 데이터를 만들어 보세요. 그러면 모든 사람이 쉽게 문제를 재현하고 안내할 수 있습니다.
Re: 열 순서가 다른 여러 테이블을 연결합니다.@uday_guntupalli
원래 순서를 저장하고 저장된 순서로 최종 테이블을 재정렬하는 구문을 작성하는 데 도움을 줄 수 있습니까?
감사해요Re: 열 순서가 다른 여러 테이블을 연결합니다.@라자트 ,
dt << Reorder By Name();
Re: 열 순서가 다른 여러 테이블을 연결합니다.감사해요 @uday_guntupalli
열의 순서를 변경하여 동일한 순서로 만들 수 있다면. 그런 다음 모든 테이블을 연결합니다.
첫 번째 테이블을 기반으로 모든 테이블의 열을 재정렬하는 구문을 작성하는 데 도움을 줄 수 있습니까? 그런 다음 루프에서 연결 명령을 사용하겠습니다. 가능하지 않은 경우 첫 번째 테이블의 원래 열 순서를 저장한 다음 모든 테이블을 해당 이름으로 다시 정렬한 다음 루프로 연결할 수 있습니다. 마지막으로 이전에 저장된 열 순서를 기반으로 최종 테이블의 순서를 변경합니다.
@Craige_Hales @txnelson 이 문제에 대한 해결책이 있나요?
감사해요. 도움을 주셔서 감사합니다.Re: 열 순서가 다른 여러 테이블을 연결합니다.@라자트 ,
ThisListAsAA = Associative Array(ThisList);
ThisListAsAAKeys =ThisListAsAA << Get Keys;
If(i == 1,
CommonItems = ThisListAsAAKeys;
,
// else
CommonItems << Intersect(ThisListAsAAKeys);
);
연관 배열에는 일대일 관계 또는 매핑이 있습니다. 따라서 연관 배열을 구성하면 두 번째 줄은 연관 배열 맵에서 고유 키를 추출하려고 시도합니다. If 블록의 경우 집합 이론을 생각해 보세요. i = 1일 때 연관 배열 1의 고유 키를 결과에 할당합니다. 그 이후로 우리는 이를 다른 연관 배열과 반복적으로 상호작용하여 마침내 모든 연관 배열에 걸쳐 공통적인 요소만 포함하는 목록에 도달합니다.
Select( :name, :age, :sex, :height, :weight ),
By Row Number
이것은 내 예의 오타였습니다. Select(CommonItems)가 적절할 것입니다.
Concatenate가 작동할지 모르겠습니다. 시도해 보세요.
Re: 열 순서가 다른 여러 테이블을 연결합니다.@uday_guntupalli
큰 도움을 주셔서 감사합니다.
댓글이 거의 없네요
- 모든 테이블에는 행 수가 다릅니다.
- 테이블을 조인하지 않도록 테이블을 연결하고 싶습니다(최종 테이블의 행 수는 개별 테이블의 행 수의 합입니다). 연결 루프가 동일하게 작동합니까?
또한 다음 명령을 이해하는 데 도움을 주시겠습니까?
- 공통 열 이름을 찾기 위한 루프입니다. 다음 줄을 설명해 주시겠습니까?
ThisListAsAA <SPAN class="token operator">=</SPAN> <SPAN class="token function">Associative Array</SPAN><SPAN class="token punctuation">(</SPAN>ThisList<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
ThisListAsAAKeys <SPAN class="token operator">=</SPAN>ThisListAsAA <SPAN class="token operator"><</SPAN><SPAN class="token operator"><</SPAN><SPAN class="token messages"> Get Keys</SPAN><SPAN class="token punctuation">;</SPAN>
<SPAN class="token function">If</SPAN><SPAN class="token punctuation">(</SPAN>i <SPAN class="token operator">==</SPAN> <SPAN class="token number">1</SPAN><SPAN class="token punctuation">,</SPAN>
CommonItems <SPAN class="token operator">=</SPAN> ThisListAsAAKeys<SPAN class="token punctuation">;</SPAN>
<SPAN class="token punctuation">,</SPAN>
<SPAN class="token comment">// else </SPAN>
CommonItems <SPAN class="token operator"><</SPAN><SPAN class="token operator"><</SPAN><SPAN class="token messages"> Intersect</SPAN><SPAN class="token punctuation">(</SPAN>ThisListAsAAKeys<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
<SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">;</SPAN>
"에서 이해한 만큼 ThisListAsAA " , " 이목록 "는 연관 배열로 저장됩니다. 다음 두 줄을 이해하는 데 도움을 주세요. - 테이블을 조인하는 루프에서 다음 줄을 설명해 주시겠습니까?
Select<SPAN class="token punctuation">(</SPAN> <SPAN class="token punctuation">:</SPAN>name<SPAN class="token punctuation">,</SPAN> <SPAN class="token punctuation">:</SPAN>age<SPAN class="token punctuation">,</SPAN> <SPAN class="token punctuation">:</SPAN>sex<SPAN class="token punctuation">,</SPAN> <SPAN class="token punctuation">:</SPAN>height<SPAN class="token punctuation">,</SPAN> <SPAN class="token punctuation">:</SPAN>weight <SPAN class="token punctuation">)</SPAN><SPAN class="token punctuation">,</SPAN>
By Row Number
열 이름이 :name, :age 등인 열이 없으며 개수가 너무 많기 때문에 열을 선택하기 위해 수동으로 쓸 수 없습니다. 또한 행 수가 다르기 때문에 행 번호로 조인할 수 없으며 조인이 아닌 테이블을 연결하려고 합니다.
감사해요
Re: 열 순서가 다른 여러 테이블을 연결합니다.@라자트 ,
나는 당신이 같은 수의 행을 가지고 있다고 가정하고 있습니다 (얼마나 유효한지 모르겠습니다).
// Define Your Directory and a list to hold File Paths
MyDirectory = "C:\" ;
nFiles = 5;
MyFilePaths = {};
ColNamesListOfList = {};
OpenDts = {};
// Test if Directory exists and get all file paths
Try( If(Directory Exists(MyDirectory),
for(i = 1 , i <= nFiles, i ++,
FilePath = Pick File( "Select File", MyDirectory);
Insert Into(MyFilePaths,FilePath);
);
,
// else
Throw()
),
"Unable to Open File";
);
// Get List of All Column Names
for(i = 1, i <= nFiles, i++,
OpenDts[i] = Open(MyDirectory || "\" || FilePath[i],"Invisible");
ColNames = OpenDts[i] << Get Column Names("String");
Insert Into(ColNamesListOfList,ColNames);
);
// Find Common Column Names
for(i = 1, i <= nFiles, i++,
ThisList = ColNamesListOfList[i];
ThisListAsAA = Associative Array(ThisList);
ThisListAsAAKeys =ThisListAsAA << Get Keys;
If(i == 1,
CommonItems = ThisListAsAAKeys;
,
// else
CommonItems << Intersect(ThisListAsAAKeys);
);
);
// Concatenate
for(i = 1, i <= nFiles, i++,
If(i == 1,
dt_Results = OpenDts[i];
,
dt_Results << Join(With( OpenDts[i]),
Merge Same Name Columns,
Select( :name, :age, :sex, :height, :weight ),
By Row Number // assuming that the tables have the same number of rows
);
);
); Re: 열 순서가 다른 여러 테이블을 연결합니다.고마워요 우데이. 공통 열을 연결하고 싶습니다.Re: 열 순서가 다른 여러 테이블을 연결합니다.@라자트 ,
설명하신 문제를 여러 단계로 나누어 살펴보겠습니다.
1단계: 원하는 파일 선택 - 파일 선택() 사용
),
"Unable to Open File";
); // Define Your Directory and a list to hold File Paths
MyDirectory = "C:\" ;
nFiles = 5;
MyFilePaths = {};
OpenDts = {};
// Test if Directory exists and get all file paths
Try( If(Directory Exists(MyDirectory),
for(i = 1 , i <= nFiles, i ++,
FilePath = Pick File( "Select File", MyDirectory);
Insert Into(MyFilePaths,FilePath);
); <BR /> , <BR /> // else<BR /> Throw()<BR />
2단계: 파일 반복, 공통 열 열기 및 연결
여기, 질문이 있습니다. 최종 테이블에 예상되는 열 목록이 있습니까? 아니면 5개 csv 파일 전체의 공통 열입니까?