将所有非null转换为小写

时间:2018-07-27 20:19:36

标签: pandas

抱歉,我是熊猫的新手。我正在尝试将数据框中的所有值都转换为小写,除非它们为null。我尝试以下方法:

public class Classy {
private String snumber;
private String spassword;
public Context context;
private boolean logged = false;

public String getSnumber() {
    return snumber;
}

public String getSpassword() {
    return spassword;
}

public void setContext(Context context) {
    this.context = context;
}

public void setSnumber(String snumber) {
    this.snumber = snumber;
}

public void setSpassword(String spassword) {
    this.spassword = spassword;
}

public boolean getLogged() {
    return logged;
}

public void setLogged(boolean logged) {
    this.logged = logged;
}

public boolean Read() {

    try {
        InputStream inputStream = context.openFileInput("COHFTW.txt");
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                stringBuilder.append(receiveString);
            }
            inputStream.close();
            String contents = stringBuilder.toString();
            if (contents.isEmpty()) {
                return false;
            } else {
                snumber = contents.substring(0, contents.indexOf("#"));
                spassword = contents.substring(contents.indexOf("#") + 1);
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("_______________File Not Found_________________");
        return false;

    } catch (IOException e) {
        System.out.println("________________File Unreadable________________");
        return false;

    }

    return true;
}


public boolean ClearFile() {


    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("COHFTW.txt", Context.MODE_PRIVATE));

        outputStreamWriter.write("");
        outputStreamWriter.close();
        System.out.println("________________________File Cleared________________");
        return true;

    } catch (Exception e) {
        System.out.println("________________Error:" + e + "________________");

        return false;

    }

}


public boolean Write() {

    System.out.println("________________Writing" + snumber + " " + spassword + "________________");

    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("COHFTW.txt", Context.MODE_PRIVATE));

        outputStreamWriter.write(snumber + "#" + spassword);
        outputStreamWriter.close();
        System.out.println("________________________Saved: " + snumber + "#" + spassword + "________________");
        return true;

    } catch (Exception e) {
        System.out.println("________________Error:" + e + "________________");

        return false;

    }

}

但出现错误

Df = df.apply (lambda x: x if pd.isna (x) else x.astype (str).str.lower() 

1 个答案:

答案 0 :(得分:0)

您可以fillna然后输入一些唯一的字符串

df.fillna('thisnanwillreplaceback').apply(lambda x :x.str.lower()).replace('thisnanwillreplaceback',np.nan)
Out[275]: 
     A    C
0  aad  aad
1  aac  aac
2  NaN  aad

简单数据

df=pd.DataFrame({'A':['AAD','AAC',np.nan],'C':['AAD','AAC','AAD']})