微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TypeError:“ float”类型的参数不可迭代-重塑csv文件

如何解决TypeError:“ float”类型的参数不可迭代-重塑csv文件


我有一个csv文件,其中第一列代表数字ID,一列代表用户评论
ID列中未包含任何注释。因此,我假装检查ID列中的每一行,如果它不是数字行,则复制该行并将其粘贴到Comments列中(ID列中的最后一个True)。print(clean_order):
web_scraper_order ...评论
0 1593612265-26203 ...伦敦是(...)
1 1593612270-26346 ...我不(...)
2 1593612265-26217 ...以及(...)
3 1593612290-26579 ...我唱歌(...)
4 1593612256-26064 ...您的第一个(...)
………… 3074 NaN ... NaN
3075国家统计局网站……NaN
3076 NaN ... NaN
3077请访问议会网站,在这里您可以下载... ... NaN
3078我实际上同意你的看法,这真是太可惜了... ... NaN

clean_order.info():

RangeIndex:3079个条目,0到3078
数据列(共16列):

列非空计数Dtype

0个web_scraper_order 2722非空对象
1个web-scraper-start-url 2324非空对象
2 discussions_Link 2141非空对象
3 discussions_Link-href 1940非空对象
4分页1820非空对象
5 Pagination-href 1757非空对象
6 Title_discussion 1720非空对象
7已发布1698个非空对象
8 Published_Date 1679非空对象
9个主题1672个非空对象
10个讨论1660非空对象
11条评论1653非空对象
12 Pagination_Comments 520非空对象
13 Pagination_Comments-href 517非空对象
dtypes:object(16) 内存使用量:192.5+ KB

我的代码

import pandas as pd

clean_order = pd.read_csv('C:/Users/(...)/Page_Clean_test.csv','w+',delimiter=';',skiprows=0,low_memory=False)
save_row = 0

for L in range(0,1500):
     if "159361" in clean_order['web_scraper_order'][L]:
         save_row = L
     else:
         clean_order['Comments'][save_row] = clean_order['Comments'][save_row] + clean_order['web_scraper_order'][L]

错误
追溯(最近一次通话过去): 文件“ C:/ Users / suiso / PycharmProjects / Teste_SA / Change web_scraper_order.py”,第12行 如果clean_order ['web_scraper_order'] [L]中为“ 159361”: TypeError:“ float”类型的参数不可迭代

解决方法

您添加.fillna()的解决方案应该可以完成工作。但是,习惯于对熊猫中的矢量化解决方案进行操作是很好的。这是另一种选择。

从此数据开始

np.random.seed(0)
df = pd.DataFrame(
    [['159361' + str(x),'first comment'] if np.random.choice([True,False])
        else ['comment' + str(x),''] for x in range(3000)],columns=['ID','Comment'])
df.loc[np.random.randint(0,3000,200),'ID'] = np.nan

>>> print(df)
               ID        Comment
0         1593610  first comment
1        comment1
2        comment2
3         1593613  first comment
4        comment4
...           ...            ...
2995  comment2995
2996  comment2996
2997   1593612997  first comment
2998   1593612998  first comment
2999  comment2999

[3000 rows x 2 columns]

现在通过获取有效ID来对ID列进行分组

coms = df.groupby(
    df.ID.str.contains('159361').cumsum() # artificial index,increases with every valid ID
    ).ID.apply(list) # returns a list of values for each valid ID

>>> print(coms)
ID
True                          [1593610,comment1,comment2]
2         [1593613,comment4,comment5,comment6,commen...
3.0                                              [15936111]
4.0                                              [15936112]
5.0                                              [15936114]
                                ...
1362.0               [1593612984,comment2986,comment2987]
1363.0                            [1593612988,comment2989]
1364.0    [1593612990,comment2991,comment2992,comment...
1365.0                                         [1593612997]
1366.0                            [1593612998,comment2999]
Name: ID,Length: 1366,dtype: object

现在将每个列表的第一个元素设置为索引,并将其余的元素加入。

coms.index = coms.str.get(0)
coms = coms.str.slice(start=1).str.join('; ')

>>> print(coms)
1593610                                      comment1; comment2
1593613       comment4; comment5; comment6; comment7; commen...
15936111
15936112
15936114
                                    ...
1593612984                             comment2986; comment2987
1593612988                                          comment2989
1593612990    comment2991; comment2992; comment2993; comment...
1593612997
1593612998                                          comment2999
Name: ID,dtype: object

附加到df中的评论列

df = df.loc[df.ID.isin(coms.index),:] \ # use only rows with valid IDs
    .set_index('ID') # and set IDs as index so it can be aligned with coms.index
df.Comment = df.Comment + '; ' + coms # join columns

>>> print(df)
ID
1593610                     first comment; comment1; comment2
1593613     first comment; comment4; comment5; comment6; c...
15936111                                      first comment;
15936112                                      first comment;
15936114                                      first comment;
...                                                       ...
1593612984            first comment; comment2986; comment2987
1593612988                         first comment; comment2989
1593612990  first comment; comment2991; comment2992; comme...
1593612997                                    first comment;
1593612998                         first comment; comment2999

[1366 rows x 1 columns]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。