1

This is my code:

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)

this is my result:

 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

how can I solve the problem? In my .xmls,these values are strings.

2
  • 1
    What exactly is your problem? What are you trying to achieve? Commented Mar 19, 2018 at 9:22
  • Thank you so much your response. The problem has solved. :) Commented Mar 19, 2018 at 9:31

1 Answer 1

6

IIUC need convert to string with lambda function:

g=df1.groupby('Attribute_spcName')['Char_spcValue'].apply(lambda x: ', '.join(x.astype(str)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.