0
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi đang thực hiện một dự án nhỏ cho chính mình, nơi tôi kết hợp hai Files CSV với
df = df.replace['e', 'x', regex=True]
1Như đã thấy trong hình dưới đây, đã có thêm dấu ngoặc
Tôi muốn xóa các trích dẫn kép khỏi tệp CSV, nhưng khi tôi sử dụng:
df = df.replace['"', '', regex=True]
chẳng có gì xảy ra.
Nếu tôi sử dụng:
df = df.replace['e', 'x', regex=True]
nó hoạt động tốt
Đây là mã của tôi:
import pandas as pd
import xlrd
import os
from pathlib import Path
import csv
#path of the file we read from
filename = os.path.join['..', 'C:\\prosjekt_lisensrapport\\201902_ModulesLicensesReport.xlsx']
sheet_names = [0,1]
dfs = pd.read_excel[filename,
sheet_name=sheet_names]
pd.concat[[df.assign[source=sheet] for sheet, df in dfs.items[]], ignore_index=True]
def sheets_to_df[filename, sheet_names]:
df_dict = pd.read_excel[filename, sheetname=sheet_names]
return pd.concat[
[df.assign[] for sheet, df in dfs.items[]], axis=1, join='inner']
df = sheets_to_df[filename, sheet_names]
df = df.replace['"', '', regex=True]
df.to_csv['testResult.csv', sep=',', encoding='utf-8']
Cảm ơn trước.
Đã hỏi ngày 13 tháng 3 năm 2019 lúc 13:32Mar 13, 2019 at 13:32
1
Bạn có thể làm điều này nhưng nó sẽ chuyển đổi các cột không chuỗi của bạn thành chuỗi một [xem bài đăng này]:
for i, col in enumerate[df.columns]:
df.iloc[:, i] = df.iloc[:, i].str.replace['"', '']
Nếu bạn không quan tâm, bạn có thể sử dụng ApplicationMap []
df = df.applymap[lambda x: x.replace['"', '']]
Hoặc đơn giản hơn nhiều, chỉ cần mở tệp và thay thế tất cả các trích dẫn kép bằng char bạn muốn. Không cần phải chèn dữ liệu của bạn vào DataFrame để làm điều này.
Tốt nhất
Đã trả lời ngày 13 tháng 3 năm 2019 lúc 13:35Mar 13, 2019 at 13:35
MaxouillemaxouilleMaxouille
2.5602 Huy hiệu vàng18 Huy hiệu bạc41 Huy hiệu đồng2 gold badges18 silver badges41 bronze badges
6
Khi các cột dựa trên chuỗi có báo giá - đôi khi chúng ta muốn loại bỏ chúng, phần lớn là vì
df = df.replace['e', 'x', regex=True]
2 về mặt kỹ thuật là một chuỗi khác với df = df.replace['e', 'x', regex=True]
3, điều này thường không phải là sự khác biệt mà chúng ta muốn tạo ra.Cho dù bạn sẽ thực hiện các từ NLP và mã thông báo [trong trường hợp đó, bạn sẽ có các mã thông báo khác nhau cho cùng một từ vì chúng "dán" .
Có khá nhiều cách để loại bỏ các trích dẫn trong một gấu trúc
df = df.replace['e', 'x', regex=True]
4.DataFrame.applymap[lambda...]
Để xóa tất cả các trích dẫn khỏi tất cả các hàng và cột của toàn bộ
df = df.replace['e', 'x', regex=True]
4, bạn có thể sử dụng df = df.replace['e', 'x', regex=True]
6 với hàm df = df.replace['e', 'x', regex=True]
7:# applymap[] works on entire DataFrame
df = df.applymap[lambda x: x.replace['"', '']]
Lưu ý: Điều này sẽ áp dụng chức năng Lambda trên mỗi hàng của mỗi cột và sẽ dẫn đến lỗi nếu không phải tất cả các cột đều thuộc loại
8. This will apply the lambda function on every row of every column, and will result in an error if not all columns are of df = df.replace['e', 'x', regex=True]
df = df.replace['e', 'x', regex=True]
8 type.Để xóa tất cả các trích dẫn khỏi tất cả các hàng trong một cột duy nhất, chỉ cần áp dụng chức năng cho một cột duy nhất:
# apply[] works on column
df['ColumnName'] = df['ColumnName'].apply[lambda x: x.replace['"', '']]
Hai cách tiếp cận này là chung chung và có thể áp dụng bất kỳ chức năng lambda nào, bên cạnh một phương pháp tận dụng
df = df.replace['e', 'x', regex=True]
9.Phương thức sê -ri 'str.replace []
Mỗi
import pandas as pd
import xlrd
import os
from pathlib import Path
import csv
#path of the file we read from
filename = os.path.join['..', 'C:\\prosjekt_lisensrapport\\201902_ModulesLicensesReport.xlsx']
sheet_names = [0,1]
dfs = pd.read_excel[filename,
sheet_name=sheet_names]
pd.concat[[df.assign[source=sheet] for sheet, df in dfs.items[]], ignore_index=True]
def sheets_to_df[filename, sheet_names]:
df_dict = pd.read_excel[filename, sheetname=sheet_names]
return pd.concat[
[df.assign[] for sheet, df in dfs.items[]], axis=1, join='inner']
df = sheets_to_df[filename, sheet_names]
df = df.replace['"', '', regex=True]
df.to_csv['testResult.csv', sep=',', encoding='utf-8']
0 cung cấp chức năng df = df.replace['e', 'x', regex=True]
8, cho phép bạn sử dụng các chức năng khác như df = df.replace['e', 'x', regex=True]
9 để thao tác các chuỗi trong các hàng của một cột duy nhất:df['ColumnName'] = df['ColumnName'].str.replace[r'"', '']
str.replace [] với regex
Để sử dụng các biểu thức thông thường với phương thức
df = df.replace['e', 'x', regex=True]
9, bạn vượt qua trong import pandas as pd
import xlrd
import os
from pathlib import Path
import csv
#path of the file we read from
filename = os.path.join['..', 'C:\\prosjekt_lisensrapport\\201902_ModulesLicensesReport.xlsx']
sheet_names = [0,1]
dfs = pd.read_excel[filename,
sheet_name=sheet_names]
pd.concat[[df.assign[source=sheet] for sheet, df in dfs.items[]], ignore_index=True]
def sheets_to_df[filename, sheet_names]:
df_dict = pd.read_excel[filename, sheetname=sheet_names]
return pd.concat[
[df.assign[] for sheet, df in dfs.items[]], axis=1, join='inner']
df = sheets_to_df[filename, sheet_names]
df = df.replace['"', '', regex=True]
df.to_csv['testResult.csv', sep=',', encoding='utf-8']
4:df['ColumnName'].replace[regex=True,inplace=False,to_replace=r'"',value=r'']
Tôi muốn thay thế một trích dẫn đơn ['] để báo giá gấp đôi ["] để làm cho nó đúng giá trị cột JSON trong Python DataFrame.
ví dụ. Tệp CSV trông giống như…
Unit Id Batch Id Items prod
A108 qa120 {'A': 123, 'B': 342, 'C': 454}
P258 re015 {'A': 124, 'B': 234, 'C': 343}
Tôi đang đọc các giá trị này từ CSV đến Pandas DataFrame. Tôi đã thử nhiều cách, nhưng không có may mắn.
df = df.replace['e', 'x', regex=True]
0Cảm ơn vì sự giúp đỡ của bạn trước đó.