python - 使用不同的分隔符拆分列

标签 python pandas split

嗨,我在 python pandas 中有一个 csv 数据框,其中有一列由三列组成。这些列用“,”分隔,但我的数据框中有一列的值如下所示:

10Bet\t\t 7.50\t5.25\t 1.34 12BET\t\t 6.90\t 4.60\t 1.38 188BET\t\t 6.00\t 5.20\t 1.38 1xBet\t\t 8.30\t 5.75\t 1.32 888sport\t\t 7.50\t 5.10\t 1.33 bet-at-home\t\t 6.64\t 5.06\t 1.35 bet365\t\t 7.50\t 5.00\t 1.36 Betclic\t\t7.50\t 4.80\t1.35 Betfair\t\t 7.50\t 4.80\t 1.36 Betsafe\t\t 7.60\t 5.25\t 1.35 Betsson\t\t 7.60\t 5.25\t 1.35 BetVictor\t\t 8.00\t 5.25\t 1.33 Betway\t\t 6.50\t 5.25\t 1.36 bwin\t\t 7.25\t 5.00\t 1.35 ComeOn\t\t 7.50\t5.25\t 1.34 Expekt\t\t7.50\t 4.80\t1.35 Interwetten\t\t8.00\t 5.30\t1.30 mybet\t\t 7.50\t 5.00\t1.35 Pinnacle\t\t 8.33 \t 5.79 \t 1.36 SBOBET\t\t 7.40\t 4.80\t 1.35 Sportingbet\t\t 7.50\t5.20\t 1.36 Tipico\t\t8.00\t 5.30\t 1.35 Unibet\t\t 7.50\t 5.10\t 1.34 William Hill\t\t 6.00\t 4.80\t 1.40 youwin\t\t 7.50\t 5.20\t 1.36 Betfair Exchange\t\t 8.41\t 5.56\t 1.37

我想将其拆分为选项卡,但有不同数量的选项卡用于分隔单元格中的值,类型为;

'\t','\t\t ' there is a space in the two tab seperator.

我尝试通过df['column'].apply(lambda x: x.split(\t))拆分它,但弹出了'float'的错误对象没有属性“split”

我想要的是在分隔符 '\t''\t\t ' 上分割的列

如何拆分此列,以便它在 pandas 或 python 中的任何其他库中显示为这样?:

10Bet 7.50 5.25 1.34 12BET 6.90 4.60 1.38 ...

最佳答案

我相信你需要split通过 \t+ - 一个或多个选项卡:

a = df['column'].str.split('\t+')
print (a)
0    [10Bet,  7.50, 5.25,  1.34 12BET,  6.90,  4.60...
Name: column, dtype: object

关于python - 使用不同的分隔符拆分列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48376093/

相关文章:

python - 如何从 Python subprocess.check_output() 捕获异常输出?

python - Python 中的“Zip”列表字典

python - sqlite3 python ATTACH DATABASE复制表.schema

python - 我可以检查 pandas dataframe 索引是否结束?

python - win32com 在使用 python 写入日期值时遇到问题

python - 具有宽/长旋转的 Groupby 累积平均值

python - 通过 for 循环在多个数据帧上运行函数

python - 使用多个分割选择文本

java - 更改 Hadoop 作业的拆分数

java - 为什么这个 split() 会失败?