나는 항상 이름의 마지막 네 자리가 같은 다른 디렉토리에서 오는 두 개의 데이터 프레임을 가지고 있습니다. 파일 경로는 다음과 같습니다.
dir1 = "path/to/files1/"
dir2 = "path/to/files2/"
그런 다음 루프를 사용하여 dataframe에 함께 속한 데이터 프레임을로드하고 연결합니다 df
.
# For each file in the first directory
for i in os.listdir(dir1):
# For each file in the second directory
for j in os.listdir(dir2):
# If the last 4 digits of filename match (ignoring file extension)
if i[-8:-4] == j[-8:-4]:
# Load CSVs into pandas
print(i[-12:-4] + ' CPU Analysis')
print('\n')
df1 = pd.read_csv(dir1 + i,delimiter=',')
df2 = pd.read_csv(dir2 + j,delimiter=';')
df = pd.concat([df1, df2])
내가 지금하고 싶은 것은 저장하는 것입니다 df
에 dir1
이전과 같은 파일 이름을 사용하여, 즉, 나는에 기존 파일을 덮어 쓰려면 dir1
CSV로 저장.
따라서 루프 끝에서 다음과 같이 사용해야한다고 생각합니다.
df.to_csv(dir1, i[:-4])
그러나 나는 이것에 대해 확신하지 못합니다.
여기에 가능한 조인 값이 있다고 생각합니다 +
.
df = pd.concat([df1, df2])
df.to_csv(dir1 + i[:-4] + '.csv', index=False)
또는 다음을 사용하십시오 f-string
.
df = pd.concat([df1, df2])
df.to_csv(f'{dir1}{i[:-4]}.csv', index=False)
그러나 원래 확장자가 필요한 경우 파일 읽기와 같은 경로를 사용하십시오.
df = pd.concat([df1, df2])
df.to_csv(dir1 + i, index=False)
df = pd.concat([df1, df2])
df.to_csv(f'{dir1}{i}', index=False)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다