question

Upvotes
Accepted
1 0 0 1

How to change pandas data type?

How to change pandas data type?

Is there a pandas function to type convert form 'pandas.core.series.Series' to 'pandas.core.frame.Pandas' ?

Data example:

numpy_data = np.array([[1, 2], [3, 4], [1, 1]])
df = pd.DataFrame(data=numpy_data, index=[1,2,3], columns=["column1", "column2"])


Here I get the pandas.core.frame.Pandas data type for every row:

for row in df.itertuples():
    print(row)
    print(type(row))


But if I read a single row I get pandas.core.series.Series type


print(type(df.iloc[1]))


How can I convert series.Series to frame.Pandas?


pythondatapandasdata-type
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

hi @t1.e

Thank you for your participation in the forum. Is the reply below satisfactory in resolving your query?
If so please can you click the 'Accept' text next to the appropriate reply? This will guide all community members who have a similar question.

Thanks,
AHS

@t1.e

Hi,

Please be informed that a reply has been verified as correct in answering the question, and marked as such.

Thanks,

AHS

1 Answer

· Write an Answer
Upvotes
Accepted
78.8k 250 52 74

@t1.e

You can try the pandas.Series.to_frame method which converts Series to DataFrame.

1640829533734.png



1640829533734.png (5.3 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.