我的pandas数据帧非常大,所以我希望能够修改textLower(frame)函数,以便它在一个命令中执行,我不必迭代每一行来对每个行执行一系列字符串操作元件。
# 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()
答案 0 :(得分:3)
试试这个:
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