拆分具有不同分隔符的列

时间:2018-01-22 06:55:26

标签: 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' object has no attribute 'split'的错误。

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

如何拆分此列,以便在pandas或python中的任何其他库中显示如下所示?:

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

1 个答案:

答案 0 :(得分:1)

我认为您需要\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 - 一个或多个标签:

<html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">

	<style>
.criteria {
	height: 60vh;
  display:flex;
	
}
.columnOne {
	width: 30%;
  display:flex;
	box-sizing: border-box;  /*whatever element it is applied to...*/
	padding: 0 0 0 10px;
}

.columnTwo {
  display:flex;
	width: 20%;
	box-sizing: border-box;  /*whatever element it is applied to...*/
	border-top: 0; border-bottom: 0;
	border-left: 3px solid blue;
	border-right: 3px solid blue;
	padding: 0 0 0 10px;
}

.columnThree {
  display:flex;
  flex-direction: column;
	width: 50%;
	box-sizing: border-box;  /*whatever element it is applied to...*/
	border-top: 0; border-bottom: 0; border-left: 0;
	padding: 0 0 0 10px;
}
.innerDiv{
  display:flex;
  height: 50%;
  width:100%
}
/* Clear floats after the columns */
.criteria:after {
	content: "";
	display: table;
	clear: both;
}

/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
@media (max-width: 600px) {
    .columnOne, .columnTwo, .columnThree {
        width: 100%;
    }
}	
	</style>
  </head>


  <body>


    <div class="criteria">
      <div class="columnOne">
        <h2>Column 1</h2>
        <select multiple name="xyz" id="xyz" size="15" style="border:none; background-color:transparent; width:100%;"></select>
      </div>
      <div class="columnTwo">
        <h2>Column 2</h2>
        <div style="overflow:auto; ">
          <select multiple name="xyz1" id="xyz1" size="15" style="border:none; background-color:transparent; width:100%;"></select>
        </div>
      </div>
      <div class="columnThree">
        <h2>Column 3</h2>
        <div class='innerDiv' style="background-color:red;">...breakup top column...</div>
        <div class='innerDiv' style="background-color:pink;">...breakup bottom column...</div>
      </div>
    </div>



    <div style="margin-left:10px;">
      <b>next section starts here :</b>
      <input id="rem" name="rem" style="width:390px">
      <br />
      <span style="font-style:italic;color:green;margin-left:130px;font-size:12px;">(nad)</span>
      <p>
        <button type="button" id="remd">btn</button><span id="Err" style="display:none;font-style:italic;color:red;margin-left:5px;">Please fix errors.</span>
        <br/>
        <p>
    </div>



  </body>

</html>