In the below code how can I iterate the lambda for rows that are not "nan", "null" or in other words blank?
The output still appends ".pdf" to even the blank rows.
model1 model2 model3
0 File1.pdf File3.pdf File2.pdf
1 File2.pdf nan.pdf File2.pdf
2 File3.pdf nan.pdf File2.pdf
model_types = []
for col in df.columns:
model_types.append(col)
df[col] = df[col].apply(lambda x: f"{x}.pdf" if x != 'nan' else x) # this part
CodePudding user response:
If you have real NaNs you should be able to add '.pdf' directly in a vectorized manner and this shouldn't impact the NaNs.
model_types = []
for col in df.columns:
model_types.append(col)
df[col] = '.pdf'
Alternatively, you can also use:
model_types = list(df)
df = df.stack().add('.pdf').unstack()
# or
df = df.apply(lambda s: s '.pdf')
Output:
model1 model2 model3
0 File1.pdf File3.pdf File2.pdf
1 File2.pdf NaN File2.pdf
2 File3.pdf NaN File2.pdf
CodePudding user response:
First convert your nans to None
.
df1 = df.where(pd.notnull(df), None)
for col in df1:
df1[col].apply(lambda x: f"{x}.pdf" if x else None)
CodePudding user response:
For people who want a more "universal" solution which let you deal with:
- 0 and integers
- floats numbers
- pd.NA = < NA >
- np.nan = NaN
Creating DF:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Col1':[0,"File2",1.23], 'Col2':["File1",pd.NA,"File3"], 'Col3':["File1","File2",np.nan]})
df
index | Col1 | Col2 | Col3 |
---|---|---|---|
0 | 0 | File1 | File1 |
1 | File2 | <NA> | File2 |
2 | 1.23 | File3 | NaN |
df1 = df.applymap(lambda x: x '.pdf' if type(x) == str else x, na_action='ignore')
df1
index | Col1 | Col2 | Col3 |
---|---|---|---|
0 | 0 | File1.pdf | File1.pdf |
1 | File2.pdf | <NA> | File2.pdf |
2 | 1.23 | File3.pdf | NaN |