TypeError:序列项3:期望的字符串,找到浮点数

时间:2018-03-19 09:12:05

标签: python pandas csv dataframe join

这是我的代码:

import pandas as pd

df1 = pd.read_csv("Book3.csv")
df2 = pd.read_csv("Book4.csv")
df2 = df2.dropna(axis=0)
g = df1.groupby('Attribute_spcName')['Char_spcValue'].apply(', '.join) #join attributes usin commas
df2['Char_spcValue'] = df2['Attribute_spcName'].map(g)
df2.rename(columns={
                 'Attribute_spcName': 'Attributes',
                 'Char_spcValue': 'Values'}, inplace=True)
df2 = df2[['Attributes', 'Values']]

df2.to_csv("AttributeHasValues.csv", index=False)

这是我的结果:

 TypeErrorTraceback (most recent call last)
    <ipython-input-1-91bb6ac8d567> in <module>()
          4 df2 = pd.read_csv("Book4.csv")
          5 df2 = df2.dropna(axis=0)
    ----> 6 g = df1.groupby('Attribute_spcName')['Char_spcValue'].apply(', '.join)
#join attributes usin commas
          7 df2['Char_spcValue'] = df2['Attribute_spcName'].map(g)
          8 df2.rename(columns={

    C:\Users\tt20172129\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\groupby.pyc in apply(self, func, *args, **kwargs)
        714         # ignore SettingWithCopy here in case the user mutates
        715         with option_context('mode.chained_assignment', None):
    --> 716             return self._python_apply_general(f)
        717 
        718     def _python_apply_general(self, f):

    C:\Users\tt20172129\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\groupby.pyc in _python_apply_general(self, f)
        718     def _python_apply_general(self, f):
        719         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
    --> 720                                                    self.axis)
        721 
        722         return self._wrap_applied_output(

    C:\Users\tt20172129\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\groupby.pyc in apply(self, f, data, axis)
       1800             # group might be modified
       1801             group_axes = _get_axes(group)
    -> 1802             res = f(group)
       1803             if not _is_indexed_like(res, group_axes):
       1804                 mutated = True

    TypeError: sequence item 3: expected string, float found

我该如何解决这个问题?在我的.xmls中,这些值是字符串。

1 个答案:

答案 0 :(得分:2)

IIUC需要使用string函数转换为lambda

g=df1.groupby('Attribute_spcName')['Char_spcValue'].apply(lambda x: ', '.join(x.astype(str)))