0

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 ?

2 Answers 2

1

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='')
Sign up to request clarification or add additional context in comments.

1 Comment

v for k is a nice trick, I've just copy-pasted your answer and it worked. Now I'm trying to understand how does it worked :D
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.