I have a DataFrame column with alphanumeric IDs - some numbers, some letters, some both. I am using read_csv to read the data and want to read all the values of this column as strings. I can't change the values in the underlying data.
I have tried to set the dtype for the column as an object
df = pd.read_csv(filename, dtype = {col: object})
I have also tried to use a converter to change all the values in the columns to strings.
df = pd.read_csv(filename, converters = {i: str for i in col})
However, I still end up with some non-string numbers (12345) and some string numbers ('12345') which mess up my aggregations.
Any suggestions? Thanks!
您也可以尝试:
df['column'] = df['column'].apply(lambda x: str(x))
Use:
df = pd.read_csv(filename, dtype = {i: str for i in col})
The only difference from this and the first one is I do dtype
not converter
, it's basically a merge of the two.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.