Shubham R
Shubham R

Reputation: 7644

Convert type(String) to pandas.core.series.Series

I have a string say

type(abc)
>>str

i want to convert it into pandas.core.series.Series.

i came across in pandas documentation that there is a code

pd.to_string()

which converts pandas series to string, but i want the opposit of it. is there any function/code to do it?

Upvotes: 3

Views: 24180

Answers (2)

Vivek Kalyanarangan
Vivek Kalyanarangan

Reputation: 9081

This is an implementation for casting a string to a series -

>>> abc = 'some string'
>>> import pandas as pd
>>> abc_series = pd.Series(abc)
>>> type(abc_series)
<class 'pandas.core.series.Series'>

Upvotes: 8

Is your string a representation of a series like "0 1\nName: My_Series, dtype: int64"? You didn't clarify. If this is what you meant I've found no direct way of doing this, though this seems to be close enough:

import io

a = "0    1\nName: My_Series, dtype: int64"
_f = io.StringIO('\n'.join(a.split('\n')[:-1]))
pd.read_csv(_f, header=None, index_col=0, sep=r'\s+')

Upvotes: 0

Related Questions