替换熊猫细胞中的角色

时间:2018-09-27 10:40:04

标签: python pandas dataframe

我有以下数据框:

Sub testDatabase()

Dim strDatabaseDirectory As String
Dim strConnectionString1 As String
Dim strConnectionString2 As String
Dim strConnectionString3 As String
Dim dbMurenaDatabase As ADODB.Connection
Dim rcsRecords As ADODB.Recordset

strDatabaseDirectory1 = ThisWorkbook.path & "\database\"
strDatabaseDirectory2 = "Libraries\Documents"
strDatabaseDirectory3 = "C:\db"
strConnectionString1 = "Data Source='" & strDatabaseDirectory & "';Delimiter=';'"
strConnectionString2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDatabaseDirectory1 & ";" & _
                        "Extended Properties=""" & "text;HDR=Yes;FMT=Delimited""" & ";"
strConnectionString3 = "Data Source='" & strDatabaseDirectory3 & "';Delimiter=';';" & _
                        "Has Quotes=True;Skip Rows=0;Has Header=True;Comment Prefix='#';" & _
                        "Column Type=String,String,String,Int32,Boolean,String,String;" & _
                        "Trim spaces=False;Ignore Empty Lines=True;"

Set dbMurenaDatabase = New ADODB.Connection
dbMurenaDatabase.Open strConnectionString3

dbMurenaDatabase.Execute "CREATE TABLE [firstTable.txt] (FirstCol TEXT, SecCol TEXT)"
dbMurenaDatabase.Execute "INSERT INTO [firstTable.txt] Values ('smth', 'smth2');"
dbMurenaDatabase.Execute "SELECT * FROM firstTable.txt"

End Sub

我想用点(“。”)替换所有逗号(“,”),其结果应如下所示:

df = pd.DataFrame(columns=["a","b","c"])
df.loc[1] = [1,2,3]
df.loc[2] = ["123.12", "3455,435",0]
df.loc[3] = [23.2, 55.5,0.1]
df.loc[4] = ["1,3","3,5","7.8"]

我该怎么做?到目前为止,我已经使用了熊猫 replace 函数,但是没有用。

1 个答案:

答案 0 :(得分:4)

使用replaceregex=True替换子字符串:

df = df.replace(',','.', regex=True)
print (df)
        a         b    c
1       1         2    3
2  123.12  3455.435    0
3    23.2      55.5  0.1
4     1.3       3.5  7.8