Python TypeError:序列项3:应为字符串,找到浮点

Python TypeError:序列项3:应为字符串,找到浮点,python,pandas,csv,dataframe,join,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['Attrib

这是我的代码:

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
我怎样才能解决这个问题?在my.xmls中,这些值是字符串。

IIUC需要使用lambda函数转换为字符串:

g=df1.groupby('Attribute_spcName')['Char_spcValue'].apply(lambda x: ', '.join(x.astype(str)))
IIUC需要使用lambda函数转换为字符串:

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

你到底有什么问题?你想达到什么目的?非常感谢你的回复。问题解决了你到底有什么问题?你想达到什么目的?非常感谢你的回复。问题解决了