2017-09-28 1 views
2

를 사용하여 천 구분자로 공간이 파일을 읽고 다음CSV 내가처럼 보이는 (프랑스어) 데이터 세트를 pandas.read_csv

import pandas as pd 
df=pd.read_csv("Example_dataset.csv", 
      index_col=0, 
      encoding='latin', 
      parse_dates=True, 
      dayfirst=True, 
      sep=';', 
      decimal=',', 
      thousands=' ') 
:

time;col1;col2;col3 
06.09.2017 05:30;329,02;5,7;259 
06.09.2017 05:40;500,5;6,6;261 
06.09.2017 05:50;521,73;6,7;266 
06.09.2017 06:00;1 091,33;9,1;273 
06.09.2017 06:10;1 262,43;10;285 

가 나는 다음과 같은 명령을 사용하여 읽으려고

col2와 col3은 float와 integer로 인식되지만 col1은 수천 개의 분리 기호로 인해 숫자로 인식되지 않습니다. 이 데이터 세트를 쉽게 읽을 수있는 방법이 있습니까?

<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 5 entries, 2017-09-06 05:30:00 to 2017-09-06 06:10:00 
Data columns (total 3 columns): 
col1 5 non-null object 
col2 5 non-null float64 
col3 5 non-null int64 
dtypes: float64(1), int64(1), object(1) 
memory usage: 160.0+ bytes 

어떤 제안 : thousands=' '을 설정하면 작동하지 않는 것? 당신은 비 분리 공백이있는 경우

+0

시도 :'df.col1 = df.col1합니다. –

+0

팬더'0.20.1'에서 방금 테스트했는데 코드가 작동합니다. 어떤 버전을 사용하고 있습니까? – zipa

+0

그건 작동하지 않았다. 이 공간은 '비 분리 공간'이라고 생각합니다. 코드를 다음과 같이 수정했습니다 : 'df.col1 = df.col1.str.replace ('\ s +', '') .str.replace (', ','.). astype (float)' – Nickj

답변

4

, 나는 str.replace로 더 공격적인 정규 표현식을 제안 :

df.col1 = df.col1.str.replace('[^\d.,e+-]', '')\ 
       .str.replace(',', '.').astype(float) 

정규식을

[  # character group 
^  # negation - ignore everything in this character group 
\d  # digit 
.  # dot 
e  # 'e' - exponent 
+-  # signs 
]  
관련 문제