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.

1 Answer

· Write an Answer
Upvotes
Accepted
80.1k 257 52 75

@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.