Python - 使用pandas在现有表中附加数据

时间:2017-10-11 17:43:00

标签: python pandas dataframe

          00          01          02          03          04
    1  (value00)   (value01)   (value02)   (value03)   (value04)
    .      .
    .      .
    .      .
  1003 (value00)   (value01)   (value02)   (value03)   (value04)

我有一个包含5列的文本文件,如上例所示。我需要使用名为Pandas的Python库将数据附加到此文本文件中。这个文件最后需要的行数大约是1003.很抱歉,如果你无法理解我的解释,我是熊猫的新人。

例如:

          00          01          02          03          04
1        4053        4665        1003        5823        5646
2        5656        5525        4561        4654        4568
3        4656        4652        4785        4987        2354
4        3262        2335        2154        5654        4658
5        5674        6584        4562        4568        5486
[...]   [...]       [...]       [...]       [...]       [...]
1003     3543        1351        3215        4532        1315

我该怎么做?

2 个答案:

答案 0 :(得分:1)

我是使用df.loc [row]来做的,它可以很容易地在循环中使用。

import pandas as pd

df = pd.DataFrame(columns=[str(i).zfill(2) for i in range(5)])
df.loc[0] = range(5) # add row 
df.loc[1] = range(5) # add row

for i in range(len(df),len(df)+2):
    df.loc[i] = range(5) # add rows to end by starting at len(df)

df

返回

    00  01  02  03  04
0   0.0 1.0 2.0 3.0 4.0
1   0.0 1.0 2.0 3.0 4.0
2   0.0 1.0 2.0 3.0 4.0
3   0.0 1.0 2.0 3.0 4.0

答案 1 :(得分:0)

将数据添加到数据框中的列。这会奏效。我认为你正在寻找更具程序性的东西。请注意,我们所有的数据都需要具有相同的大小,因此这样的附加是繁琐的,但它会起作用。

<div class="container group">

  <article>
    <header class="image">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/54/Montauk_Point_Lighthouse.jpg/300px-Montauk_Point_Lighthouse.jpg" width="300" height="200" alt="Fionna">
    </header>
    <div class="text">
      <h3>Fionna</h3>
      <p>An alternate, female version of Finn, Fionna the human is just as brave, adventurous and awesome as her male counterpart as she faces off against her enemy, the Ice Queen.</p>
    </div>
  </article>


  <article>
    <header class="image">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/54/Montauk_Point_Lighthouse.jpg/300px-Montauk_Point_Lighthouse.jpg" width="300" height="200" alt="Peppermint Butler">
    </header>
    <div class="text">
      <h3>Peppermint Butler</h3>
      <p>Peppermint Butler is an inhabitant of the Candy Kingdom and loyal butler to Princess Bubblegum. He has a mysterious past and an undefined relationship with Death.</p>
    </div>
  </article>


  <article>
    <header class="image">
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/54/Montauk_Point_Lighthouse.jpg/300px-Montauk_Point_Lighthouse.jpg" width="300" height="200" alt="Flame Princess">
    </header>
    <div class="text">
      <h3>Flame Princess</h3>
      <p>Flame Princess is a hot-headed princess from the Fire Kingdom and Finn's new crush. Her flame powers are tied to her emotions, and she's been known to anger quite easily.
        <p>
    </div>
  </article>
</div>

如果形状不同,您可能会看到此错误import pandas as pd df = pd.DataFrame({'00': ['value1', 'value2', 'value3'], '01': ['value4', 'value5', 'value6'], '02': ['value7', 'value8', 'value9'], '03': ['value10', 'value11', 'value12'], '04': ['value13', 'value14', 'value15']}) In[2]: df Out[2]: 00 01 02 03 04 0 value1 value4 value7 value10 value13 1 value2 value5 value8 value11 value14 2 value3 value6 value9 value12 value15