划定特定列并将它们添加为CSV(Python3,CSV)列

我有一个csv文件,它有几列,我首先用冒号(;)分隔。 但是,ONE列由管道分隔 我想分隔这个列并创建新的列。

输入:

  Column 1    Column 2      Column 3
     1           2          3|4|5
     6           7          6|7|8
     10          11         12|13|14

期望的输出:

  Column 1   Column 2      ID    Age  Height
     1          2          3      4    5 
     6          7          6      7    8
     10         11         12     13   14

我的代码到目前为止第一次通过; 然后转换为DF(这是我想要的结束格式)

delimit = list(csv.reader(open('test.csv', 'rt'), delimiter=';'))
df = pd.DataFrame(delimit)

你没有精确显示数据的外观(你说它用分号分隔,但你的例子没有),但如果它看起来像

Column 1;Column 2;Column 3
1;2;3|4|5
6;7;6|7|8
10;11;12|13|14

你可以做类似的事情

>>> df = pd.read_csv("test.csv", sep="[;|]", engine='python', skiprows=1, 
                     names=["Column 1", "Column 2", "ID", "Age", "Height"])
>>> df
   Column 1  Column 2  ID  Age  Height
0         1         2   3    4       5
1         6         7   6    7       8
2        10        11  12   13      14

这可以通过使用正则表达式分隔符来表示,即“或;| ”并手动强制列名。

或者,您可以通过几个步骤来完成:

>>> df = pd.read_csv("test.csv", sep=";")
>>> df
   Column 1  Column 2  Column 3
0         1         2     3|4|5
1         6         7     6|7|8
2        10        11  12|13|14
>>> c3 = df.pop("Column 3").str.split("|", expand=True)
>>> c3.columns = ["ID", "Age", "Height"]
>>> df.join(c3)
   Column 1  Column 2  ID Age Height
0         1         2   3   4      5
1         6         7   6   7      8
2        10        11  12  13     14

delimit = list(csv.reader(open('test.csv', 'rt'), delimiter=';'))

for row in delimit:
    piped = row.pop()
    row.extend(piped.split('|'))

df = pd.DataFrame(delimit)

delimit最终看起来像:

[
    ['1', '2', '3', '4', '5'],
    ['6', '7', '6', '7', '8'],
    ['10', '11', '12', '13', '14'],
]

实际上使用csv lib和str.replace要快得多:

import csv
with open("test.txt") as f:
    next(f)
    # itertools.imap python2
    df = pd.DataFrame.from_records(csv.reader(map(lambda x: x.rstrip().replace("|", ";"), f), delimiter=";"),
                                   columns=["Column 1", "Column 2", "ID", "Age", "Height"]).astype(int)

一些时间:

In [35]: %%timeit
pd.read_csv("test.txt", sep="[;|]", engine='python', skiprows=1,
                     names=["Column 1", "Column 2", "ID", "Age", "Height"])
   ....: 
100 loops, best of 3: 14.7 ms per loop

In [36]: %%timeit                                                             
with open("test.txt") as f:
    next(f)
    df = pd.DataFrame.from_records(csv.reader(map(lambda x: x.rstrip().replace("|", ";"), f),delimiter=";"),
                               columns=["Column 1", "Column 2", "ID", "Age", "Height"]).astype(int)
   ....: 
100 loops, best of 3: 6.05 ms per loop

你可以str.split:

with open("test.txt") as f:
    next(f)
    df = pd.DataFrame.from_records(map(lambda x: x.rstrip().replace("|", ";").split(";"), f),
                                   columns=["Column 1", "Column 2", "ID", "Age", "Height"])
链接地址: http://www.djcxy.com/p/30321.html

上一篇: Delimit a specific column and add them as columns in CSV (Python3, CSV)

下一篇: How do I use python