반응형
CSV 파일을 가져올 때 선행 0을 유지하는 방법
방법 :
DataFrame을 CSV 파일로 저장할 때 데이터의 leading zero(선행 0)를 유지하는 방법은
열의 데이터를 문자열로 변환하는 것
import pandas as pd
# 예제 DataFrame 생성
data = {'Column1': ['001', '002', '003'], 'Column2': [10, 20, 30]}
df = pd.DataFrame(data)
# 모든 열을 문자열로 변환
df = df.astype(str)
# DataFrame을 CSV로 저장
df.to_csv('output.csv', index=False)
특정 열만 문자열로 변환
특정 열만 문자열로 변환하여 선행 0을 유지하려는 경우:
import pandas as pd
# 예제 DataFrame 생성
data = {'Column1': ['001', '002', '003'], 'Column2': [10, 20, 30]}
df = pd.DataFrame(data)
# 특정 열을 문자열로 변환
df['Column1'] = df['Column1'].astype(str)
# DataFrame을 CSV로 저장
df.to_csv('output.csv', index=False)
How to keep leading zeros when writing to CSV file in python
I have some code that reads through a list of HTML files, takes some information from each of these, and then presents them in a CSV file. Part of this is using the number from the file's name to
stackoverflow.com
반응형
'Python' 카테고리의 다른 글
DataFrame 에서 엑셀 파일을 가져올 때 선행 0을 유지하는 방법 (1) | 2024.07.16 |
---|---|
VCF 파일에서 missing allele 분석하는 방법 (0) | 2024.07.14 |
Multiple VCF 파일을 하나의 DataFrame 으로 합치기 (0) | 2024.07.14 |
Pandas를 활용한 VCF 파일 분석 방법 (심화) (0) | 2024.07.14 |
Pandas를 활용한 VCF 파일 전처리 분석 방법 (1) | 2024.07.14 |