在现有的数据框中添加多行

4

你好,我正在学习数据科学,并尝试从涉及各行业的公司名单中列出大数据公司清单。

我有一个名为comp_rows的大数据公司行号列表。现在,我想根据行号过滤公司并创建一个新的数据框架。在这里,我需要向现有的数据框架添加行,但出现了错误。有人能帮忙吗?

我的数据框架看起来像这样。

    company_url company tag_line    product data
0   https://angel.co/billguard  BillGuard   The fastest smartest way to track your spendin...   BillGuard is a personal finance security app t...   New York City · Financial Services · Security ...
1   https://angel.co/tradesparq Tradesparq  The world's largest social network for global ...   Tradesparq is Alibaba.com meets LinkedIn. Trad...   Shanghai · B2B · Marketplaces · Big Data · Soc...
2   https://angel.co/sidewalk   Sidewalk    Hoovers (D&B) for the social era    Sidewalk helps companies close more sales to s...   New York City · Lead Generation · Big Data · S...
3   https://angel.co/pangia Pangia  The Internet of Things Platform: Big data mana...   We collect and manage data from sensors embedd...   San Francisco · SaaS · Clean Technology · Big ...
4   https://angel.co/thinknum   Thinknum    Financial Data Analysis Thinknum is a powerful web platform to value c...   New York City · Enterprise Software · Financia...

我的代码如下:

bigdata_comp = DataFrame(data=None,columns=['company_url','company','tag_line','product','data'])

for count, item in enumerate(data.iterrows()):
    for number in comp_rows:
        if int(count) == int(number):
            bigdata_comp.append(item)

错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-234-1e4ea9bd9faa> in <module>()
      4     for number in comp_rows:
      5         if int(count) == int(number):
----> 6             bigdata_comp.append(item)
      7 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.pyc in append(self, other, ignore_index, verify_integrity)
   3814         from pandas.tools.merge import concat
   3815         if isinstance(other, (list, tuple)):
-> 3816             to_concat = [self] + other
   3817         else:
   3818             to_concat = [self, other]

TypeError: can only concatenate list (not "tuple") to list

可能有一种方法可以使用索引或布尔索引而不使用循环来完成此操作。请发布您所需的输出以进行澄清。 - Bob Haffner
谢谢!fixxxer 给我解释得非常好。 - pythonlearner
2个回答

8

看起来您正在尝试根据索引(存储在变量comp_rows中)过滤现有数据框。 您可以使用loc而无需使用循环来完成此操作,如下所示:

In [1161]: df1.head()
Out[1161]: 
          A         B         C         D
a  1.935094 -0.160579 -0.173458  0.433267
b  1.669632 -1.130893 -1.210353  0.822138
c  0.494622  1.014013  0.215655  1.045139
d -0.628889  0.223170 -0.616019 -0.264982
e -0.823133  0.385790 -0.654533  0.582255

我们将获取所有列的索引为'a'、'b'和'c'的行:
In [1162]: df1.loc[['a','b','c'],:]
Out[1162]: 
          A         B         C         D
a  1.935094 -0.160579 -0.173458  0.433267
b  1.669632 -1.130893 -1.210353  0.822138
c  0.494622  1.014013  0.215655  1.045139

你可以在这里了解更多相关信息。

关于你的代码:

1. 你不需要遍历列表来查看其中是否存在某个项: 使用in运算符。例如 -

In [1199]: 1 in [1,2,3,4,5]
Out[1199]: True

所以,与其使用

标签,
for number in comp_rows:
        if int(count) == int(number):

执行此操作

if number in comp_rows

2. pandas中的append不是原地修改。您必须将结果存储到另一个变量中。请参见此处

3.

逐行追加是实现您想要的方式很慢的一种方法。相反,将要添加的每一行保存到一个列表中,然后制作一个数据帧,并在一次操作中将其附加到目标数据帧中。类似于这样..

temp = []
for count, item in enumerate(df1.loc[['a','b','c'],:].iterrows()):
    # if count in comp_rows:
    temp.append( list(item[1]))

## -- End pasted text --

In [1233]: temp
Out[1233]: 
[[1.9350940285526077,
  -0.16057932637141861,
  -0.17345827000000605,
  0.43326722021644282],
 [1.66963201034217,
  -1.1308932586268696,
  -1.2103527446031515,
  0.82213753819050794],
 [0.49462218161377397,
  1.0140133740187862,
  0.2156547595968879,
  1.0451391564351897]]

In [1236]: df2 = df1.append(pd.DataFrame(temp, columns=['A','B','C','D']))

In [1237]: df2
Out[1237]: 
          A         B         C         D
a  1.935094 -0.160579 -0.173458  0.433267
b  1.669632 -1.130893 -1.210353  0.822138
c  0.494622  1.014013  0.215655  1.045139
d -0.628889  0.223170 -0.616019 -0.264982
e -0.823133  0.385790 -0.654533  0.582255
f -0.872135  2.938475 -0.099367 -1.472519
0  1.935094 -0.160579 -0.173458  0.433267
1  1.669632 -1.130893 -1.210353  0.822138
2  0.494622  1.014013  0.215655  1.045139

0

请替换以下行:

for count, item in enumerate(data.iterrows()):

通过

for count, (index, item) in enumerate(data.iterrows()):

或者甚至只是

for count, item in data.iterrows():

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接