본문 바로가기

공부/개발

내가 보려고 쓰는 python 다룰 때 사소하지만 계속 찾아보는 것들

반응형

 

csv 파일 불러오기

{pandas dataframe 이름}.read_csv('{파일 이름.csv}')

ex) goemotion_train = pd.read_csv('data/goemotions_train_with_guid.csv')

 

tsv 파일 불러오기

{pandas dataframe 이름}.read_csv('{파일 이름.tsv}', sep="\t")

ex)goemotion_test = pd.read_csv('data/goemotions_test.tsv', sep="\t", header=None, names=['context','emotion', 'w'])

 


불필요한 열 지울때

{pandas dataframe 이름}.drop(labels = '{열 이름}', axis=1)

ex) goemotion_test = goemotion_test.drop(labels = 'Unnamed: 0', axis=1)

 

해당 값의 카운터를 알고 싶을때

{pandas dataframe 이름}.{보고싶은 열 값}..value_counts()

ex) out.emotion.value_counts()

 

데이터 프레임의 값을 카운트하고 싶을때

a = pd.DataFrame(prediction)
print(a.value_counts())

 

numpy.addrayy

=> .shape

 

list 

import numpy as np
X = np.array([texts])
X.shape

 

데이터 전처리

데이터 타입들을 바꾸고 싶을 때

# 전체

data = data.astype('int')

# 부분만

PR_coding_df['pr'] = PR_coding_df['pr'].astype('string')

 

df 합치기

data = pd.concat([survey_positive, survey_negative, amazon_negative, amazon_positive])

 

Nan to zeor(Nan 값을 0으로 바꾸고 싶을 때)

df.fillna(0)

 

numpy array 데이터 타입 확인

arange로 numpy array 만들기

import numpy as np

arr = np.arange(10)

dtype으로 numpy array 데이터 타입 확인하기

arr.dtype

 

array to string

def listToString(s): 
    # initialize an empty string
    str1 = " " 
    # return string  
    return (str1.join(s))
        
# Driver code    
s = ['Geeks', 'for', 'Geeks']
print(listToString(s))
def listToString(s): 
    str1 = " " 
    # return string  
    return (str1.join(s))
# Driver code    
s = ['Geeks', 'for', 'Geeks']
print(listToString(s)) 

 

[ERROR]

encoding error

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x98 in position 595: invalid start byte
Past=pd.read_csv("C:/Users/.../Past.csv",encoding='utf-8') 
Past=pd.read_csv("C:/Users/.../Past.csv",encoding='cp1252')

UTF-8 -> cp1252

그래도 다른 에러가 뜨면?

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 596: character maps to <undefined>
pd.read_csv("Your filename", encoding="ISO-8859-1")

 

반응형