2016-06-08 5 views
2

내 팬더 ​​데이터 프레임이 매우 커서 한 명령으로 실행되도록 textLower (프레임) 함수를 수정할 수 있어야합니다. 각 행에 대해 반복하여 각 요소에 대해 일련의 문자열 조작을 수행합니다.개별 요소를 반복하지 않고 팬더 데이터 프레임의 텍스트에 사용자 정의 함수 적용

# Function iterates over all the values of a pandas dataframe 
def textLower(frame): 
    for index, row in frame.iterrows(): 
     row['Text'] = row['Text'].lower() 
     # further modification on row['Text'] 
    return frame 


def tryLower(): 
    cities = ['Chicago', 'New York', 'Portland', 'San Francisco', 
    'Austin', 'Boston'] 
    dfCities = pd.DataFrame(cities, columns=['Text']) 
    frame = textLower(dfCities) 

    for index, row in frame.iterrows(): 
     print(row['Text']) 
######################### main() #########################  
def main(): 
    tryLower() 

답변

2

이 시도 :

dfCities["Text"].str.lower() 

나이 :

def textLower(x): 
    return x.lower() 

dfCities = dfCities["Text"].apply(textLower) 
dfCities 

# 0   chicago 
# 1   new york 
# 2   portland 
# 3 san francisco 
# 4   austin 
# 5   boston 
# Name: Text, dtype: object 
+0

을이 작품 예 ...! 도와 주셔서 감사합니다. – Bonson

관련 문제