To get a Series from a namedtuple you could use the _fields attribute:
In [11]: pd.Series(a, a._fields)
Out[11]:
ticker GE
date 2010-01-01
price 30
dtype: object
Similarly you can create a DataFrame like this:
In [12]: df = pd.DataFrame(l, columns=l[0]._fields)
In [13]: df
Out[13]:
ticker date price
0 GE 2010-01-01 30
1 GE 2010-01-02 31
You have to set_index after the fact, but you can do this inplace:
In [14]: df.set_index(['ticker', 'date'], inplace=True)
In [15]: df
Out[15]:
price
ticker date
GE 2010-01-01 30
2010-01-02 31
Answer from Andy Hayden on Stack OverflowTo get a Series from a namedtuple you could use the _fields attribute:
In [11]: pd.Series(a, a._fields)
Out[11]:
ticker GE
date 2010-01-01
price 30
dtype: object
Similarly you can create a DataFrame like this:
In [12]: df = pd.DataFrame(l, columns=l[0]._fields)
In [13]: df
Out[13]:
ticker date price
0 GE 2010-01-01 30
1 GE 2010-01-02 31
You have to set_index after the fact, but you can do this inplace:
In [14]: df.set_index(['ticker', 'date'], inplace=True)
In [15]: df
Out[15]:
price
ticker date
GE 2010-01-01 30
2010-01-02 31
Calling the DataFrame constructor on the list of namedtuples produce a dataframe:
df = pd.DataFrame(l)
ticker date price
0 GE 2010-01-01 30.0
1 GE 2010-01-02 31.0
Calling set_index() on the result produces the desired output. However, since OP doesn't want that, another way could be to convert each namedtuple into a dictionary and pop keys.
l_asdict = [x._asdict() for x in l]
df = pd.DataFrame(l_asdict, index=pd.MultiIndex.from_arrays([[x.pop(k) for x in l_asdict] for k in ['ticker', 'date']], names=['ticker', 'date']))
price
ticker date
GE 2010-01-01 30.0
2010-01-02 31.0
Videos
df2 = pd.DataFrame(index=df1.index)
This will create a DataFrame with no columns but just an index, and it will be the same index as in the df1.
It's better to set index as df1.index.copy()
df2 = pd.DataFrame(index=df1.index.copy())
You can use df1.index is df2.index to check whether they are the same object
Could you please try this:
df.reset_index(inplace = True, drop = True)
Let me know if this works.
When you are reading in the csv, use pandas.read_csv(index_col= #, * args). If they don't have a proper index column, set index_col=False.
To change indices of an existing DataFrame df, try the methods df = df.reset_index() or df=df.set_index(#).