简体   繁体   English

如何为一组 pandas dataframe 正确迭代每一行

[英]How to properly iterate over each row for a set of pandas dataframe

I am trying to apply strip() function to all rows for a set of pandas dataframes, I am trying to figure out how to convert this set of dataframes into a class and then apply a strip() function, as the error is the next one: I am trying to apply strip() function to all rows for a set of pandas , I am trying to figure out how to convert this set of dataframes into a class and then apply a strip() function, as the error is the next一:

AttributeError: 'DataFrame' object has no attribute 'strip'

Here's my attempt to iterate over each row:这是我尝试遍历每一行的尝试:

for df in (df1, df2):
    df1 = df1.strip()
    df2 = df2.strip()   

data数据

import pandas as pd

df1= pd.DataFrame(data={'col1': [' hey ' , ' world ', "-"], 'col2': [' hello ' , "-", ' world ']})
df2 = pd.DataFrame(data={'col3': [' brazil ' , ' china ', "-"], 'col4': [' russia ' , "-", ' india ']})

Is there any way to accomplish this task?有没有办法完成这个任务?

Try (without the for loop):尝试(没有 for 循环):

df1 = df1.apply(lambda x: x.str.strip())
df2 = df2.apply(lambda x: x.str.strip())

Or a bit less verbose:或者不那么冗长:

strip = lambda s: s.str.strip()

df1.apply(strip)
df2.apply(strip)

Or with replace :replace

trailings = ['^\s+', '\s+$']
df1.replace(trailings, '', regex=True)
df2.replace(trailings, '', regex=True)

If you want to use loop, then update the data of the dataframe, instead of reassigning them:如果要使用循环,则更新 dataframe 的数据,而不是重新分配它们:

list_df = [df1, df2]
for df in [df1,df2]:
    # df = df.apply(strip) wouldn't work
    df[:] = df.apply(strip)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM