2

I would like to put all the dataframes' rows in lower case. I am considering multiple dataframes, so I am doing a for loop through them. I have tried as follows

for i, file in enumerate(files):
        df[str(i)]= pd.read_csv(file)
        df[str(i)].apply(lambda x: x.astype(str).str.lower())

but unfortunately it is not returning rows in lower case. I have follower the answer given in a previous post: Convert whole dataframe from lower case to upper case with Pandas

Could you please tell me what it is wrong in the code above? Thanks

still_learning
  • 776
  • 9
  • 32

3 Answers3

1

It looks like you're putting your DataFrames into a dictionary; that definitely helps.
But you have to assign the result of the .apply() operation to something.
As it is it's not being saved anywhere.
Try instead (with df renamed to be more clear):

df_dict = {}
for i, f in enumerate(files):
    df_dict[str(i)] = pd.read_csv(f)
    df_dict[str(i)] = df_dict[str(i)].apply(lambda x: x.astype(str).str.lower())
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
1

Can you try to use it this way:

df[str(i)] = df[str(i)].apply(lambda x: x.astype(str).str.lower())
Anshul
  • 1,413
  • 2
  • 6
  • 14
0

It is more "clean" (and quicker) approach to operate only on string columns.

Pandas has select_dtypes method, where you can pass include='object' parameter, to get columns of object type (actually string).

But application of your lambda function to such a DataFrame (result of select_dtypes) alone only generates the lower-cased content, but does not save it anywhere.

So to convert the columns in question you should run update with the result of both these functions (select_dtypes and apply):

df.update(df.select_dtypes(include='object')\
    .apply(lambda x: x.astype(str).str.lower()))

And as I suppose, after that you should save the current DataFrame back to the source file.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41