在pandas dataframe中以逗号分隔的字符串中的每个项目添加+1

时间:2015-08-26 22:44:21

标签: python pandas

我有一个pandas数据框,结构如下:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>

      </button>
      <div class="navbar-inline"> <a class="navbar-brand hidden-xs" href="/Home">Yoga</a>

        <input type="text" class="form-control" placeholder="Search" id="navbarSearchQuery" name="location">
      </div>
    </div>
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Browse <span class="caret"></span></a>

          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Popular</a>

            </li>
            <li><a href="#">Friends</a>

            </li>
            <li><a href="#">Groups</a>

            </li>
            <li><a href="#">Neighborhoods</a>

            </li>
            <li><a href="#">About <span class="sr-only">(current)</span></a>

            </li>
            <li><a href="#">Contact</a>

            </li>
          </ul>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>

对于数据框中的每一行,我想在“开始”列中为每个值添加1。 “开始”列的dtype是&#39; object&#39;。

所需的输出如下所示:

|    ID    |     Start    |    Stop    |
________________________________________
|     1    |    1,2,3,4   |  5,6,7,7   |
|     2    |    100,101   |  200,201   |

我已经尝试了以下(以及以下的许多版本),但得到一个错误说明,TypeError:无法连接&#39; str&#39;和&#39; int&#39;对象,:

|    ID    |     Start    |    Stop    |
________________________________________
|     1    |    2,3,4,5   |  5,6,7,7   |
|     2    |    101,102   |  200,201   |

我尝试将列强制转换为int,但得到基数为10的long()的文字无效:&#39; 101,102&#39;:

 df['test'] = [str(x + 1) for x in df['Start']]

我尝试使用str.split()将字段转换为列表,然后将每个项目转换为整数:

提前致谢!

2 个答案:

答案 0 :(得分:1)

通过告诉您无法连接字符串和int对象,您知道x必须是字符串。你可以通过在向它添加1之前将x转换为int来解决这个问题。因此str(x+1)变为str(int(x)+1)

df['test'] = [str(int(x) + 1) for x in df['Start']]

答案 1 :(得分:1)

df['Start']是整个系列,所以你必须迭代它,然后拆分:

new_series = []
for x in df['Start']:
    value_list = []
    for y in x.rstrip(',').split(','):
        value_list.append(str(int(y) + 1))
    new_series.append(','.join(value_list))
df['test'] = new_series