1

I have a DataFrame d1 with strings and missing values, such as

d1 = pd.DataFrame([["A", "B", "C"],
                   ["D", np.nan, "F"],
                   ["G", "H", "I"],],
                  columns=[1, 2, 3])

enter image description here

whose columns I would like to aggregate in single-row DataFrame d2:

enter image description here

Folllowing suggestions in a previous post, tried the following code:

d2 = d1.agg(''.join).to_frame().T

Still, as one of the values in d1 was missing (and, thus, a float), I got the following error:

TypeError: sequence item 1: expected str instance, float found

Would you know how to change missing values in DataFrames to another data type such as string?

1

3 Answers 3

1

You can fill the missing value with an empty string:

d1.fillna('')

So the overall code becomes

d1.fillna('').agg(''.join).to_frame().T
     1   2    3
0  ADG  BH  CFI
Sign up to request clarification or add additional context in comments.

Comments

1

You can do a replace for nan values into ''

d1 = pd.DataFrame([["A", "B", "C"],
                   ["D", np.nan, "F"],
                   ["G", "H", "I"],],
                  columns=['1', '2', '3'])
d1.replace(np.nan,'',inplace=True)
d2 = d1.agg(''.join,axis=1).to_frame().T

Comments

1

The null value is causing the error, so fill it with empty string. You could try this:

d2 = pd.DataFrame(d1.fillna('').agg(''.join)).T
print(d2)

     1   2    3
0  ADG  BH  CFI

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.