我的熊猫数据框非常大,所以我希望能够修改 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()
Run Code Online (Sandbox Code Playgroud)
尝试这个:
dfCities["Text"].str.lower()
Run Code Online (Sandbox Code Playgroud)
或这个:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1445 次 |
| 最近记录: |