pd.read_csv 忽略括号内的逗号

2024-04-17

我有一个非常简单的文件:

[Name]
Streamline 1


[Data]
X [ m ], Y [ m ], Z [ m ], Velocity [ m s^-1 ]
2.66747564e-01, 0.00000000e+00, 2.03140453e-01, (0.00000000e+00, 8.17744827e+00, 0.00000000e+00)
2.66958952e-01, 0.00000000e+00, 2.07407191e-01, (0.00000000e+00, 6.77392197e+00, 0.00000000e+00)
2.63460875e-01, 0.00000000e+00, 2.06593186e-01, (0.00000000e+00, 7.04168701e+00, 0.00000000e+00)
2.65424699e-01, 0.00000000e+00, 2.00831652e-01, (0.00000000e+00, 8.93691921e+00, 0.00000000e+00)
2.70607203e-01, 0.00000000e+00, 2.02286631e-01, (0.00000000e+00, 8.45830917e+00, 0.00000000e+00)
2.68299729e-01, 0.00000000e+00, 1.97365344e-01, (0.00000000e+00, 1.00771456e+01, 0.00000000e+00)
...

我需要将速度作为向量加载到单行中。

我的基本代码:

df = pd.read_csv("C:/Users/Marek/Downloads/0deg-5ms.csv", skiprows=5)

但这种尝试导致第 2 列成为索引,其余的分成 4 列。index_col=False可以解决索引的问题,但是会导致索引超出范围。我需要一个分隔符来隐式告诉 pandas 忽略括号中的任何内容。我想python在读取csv文件时忽略带括号的分隔符 https://stackoverflow.com/questions/58087497/python-ignore-the-separator-withing-brackets-while-reading-a-csv-file可能有用,但是是的,我到处都有空间。我找到了一些使用扩展函数来加载文件并按行处理的解决方案,例如包含括号中偶尔出现逗号的列的 CSV 文件会导致 pandas.read_csv 崩溃 https://stackoverflow.com/questions/55580933/csv-file-containing-column-with-occasional-comma-in-parentheses-crashes-pandas-r and 将带有括号括起来的数据的 CSV 加载到 pandas 数据框中 https://stackoverflow.com/questions/46259548/load-csv-with-data-surrounded-by-parentheses-into-a-pandas-dataframe。然而,我相信这是一个非常简单的场景,因为所有行都是相似的,可以通过单行添加来解决delimiter='some_regex'。然而我无法弄清楚这个正则表达式应该是什么样子。它应该寻找分隔符,但不是(.*,.*).

我尝试过以下操作,但这会产生一列:

df = pd.read_csv("C:/Users/Marek/Downloads/0deg-5ms.csv", skiprows=5,  delimiter=',^(\(.*,.*\))')

编辑:得到这样的东西 -,|(?:(\(.*,.*\))),但这会在每个逗号后添加一个空列。


您可以手动解析该文件:

data = []
with open('data.csv') as fp:
    [next(fp) for i in range(5)]  # skiprows=5
    headers = [c.strip() for c in next(fp).split(',')]
    for line in fp:
        data.append([i.strip() for i in re.split(r',(?![^\(]*[\)])', line)])

df = pd.DataFrame(data, columns=headers).apply(pd.eval)

Output:

>>> df
    X [ m ]  Y [ m ]   Z [ m ]     Velocity [ m s^-1 ]
0  0.266748      0.0  0.203140  [0.0, 8.17744827, 0.0]
1  0.266959      0.0  0.207407  [0.0, 6.77392197, 0.0]
2  0.263461      0.0  0.206593  [0.0, 7.04168701, 0.0]
3  0.265425      0.0  0.200832  [0.0, 8.93691921, 0.0]
4  0.270607      0.0  0.202287  [0.0, 8.45830917, 0.0]
5  0.268300      0.0  0.197365  [0.0, 10.0771456, 0.0]

>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 4 columns):
 #   Column               Non-Null Count  Dtype  
---  ------               --------------  -----  
 0   X [ m ]              6 non-null      float64
 1   Y [ m ]              6 non-null      float64
 2   Z [ m ]              6 non-null      float64
 3   Velocity [ m s^-1 ]  6 non-null      object 
dtypes: float64(3), object(1)
memory usage: 320.0+ bytes

>>> type(df.iloc[0, 3])  # [0.0, 8.17744827, 0.0]
list

>>> type(df.iloc[0, 3][1])  # 8.17744827
float
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pd.read_csv 忽略括号内的逗号 的相关文章

随机推荐