Reputation: 736
Goal:
I'm creating charts for 2-d data with different lengths using Pandas. I need to create a new serie for each table. Data structure is like;
|---------------------|------------------|
| A | 4 |
|---------------------|------------------|
| B | 34 |
|---------------------|------------------|
When I know how many lines will return from data source I can create Pandas series like;
raw_serie = pd.Series([raw_data['A'], raw_data['B']], index=['A', 'B'], name='')
Problem:
What if different lengts of data returns from the data source ? How can I create dynamic Pandas series for diffent row lengths automatically ?
Upvotes: 0
Views: 1335
Reputation: 1661
A pd.Series is just a column, if you want to create a table, you need to create pd.DataFrame. The best practice is to create the pd.DataFrame first and then you can add some rows. In your case, you don't know the lenght of the rows you want to insert. So you can add your data as new columns and then pivot the dataframe so that your columns become rows.
Upvotes: 0
Reputation: 2211
Just pass in the data as a list comprehension..
raw_datav = {'A': 4, 'B': 34}
raw_serie = pd.Series([v for k, v in raw_datav.items()], index=[k for k, v in raw_datav.items()], name='')
Upvotes: 1