【发布时间】:2020-01-30 10:33:08
【问题描述】:
我将整个表格作为字符串,如下所示: a= "id;date;type;status;description\r\n1;20-Jan-2019;cat1;active;customer is under\xe9e 观察\r\n2;18-Feb-2019;cat2;active;customer is正版\r\n"
在字符串内部,我们确实有一些 ascii 代码,例如 \xe9e,所以我们必须将字符串转换为非 ascii
我的预期输出是将上面的字符串转换为数据框 如下:
id date type status description
1 20-Jan-2019 cat1 active customer is under observation
2 18-Feb-2019 cat2 active customer is genuine
我的代码:
b = a.splitlines()
c = pd.DataFrame([sub.split(";") for sub in b])
我得到以下输出。但我需要第一行作为我的标题,并将 ascii 转换为 utf-8 文本。
0 1 2 3 4 5 6
0 id date type status description None None
1 1 20-Jan-2019 cat1 active customer is underée observation None None
2 2 18-Feb-2019 cat2 active customer is genuine None None
另外,请不要在这里创建值为 None 的额外列。不应该是这样的
【问题讨论】:
-
您尝试过什么,遇到了什么具体问题?
-
c.columns = c.iloc[0],然后是c = c.iloc[1:].reset_index(drop=True) -
@Erfan 我错过了一行要在此处更新,它正在创建具有值 None 的额外列
-
是
None还是NaN?
标签: python string pandas python-2.7