S
S
san_m_m2021-07-30 14:43:13
Python
san_m_m, 2021-07-30 14:43:13

Question on combining pandas and seriaes?

Good afternoon!

I can’t think of something
There is a DataFrame

state = ['California', 'Texas', 'New York', 'Florida', 'Illinois']
area = [423967, 695662, 141297,  170312, 149995]
cl = ['tropic',  'tropic', 'norm', 'tropic', 'norm']
pop = [ 38332521, 26448193, 19651127, 19552860, 12882135]
data = pd.DataFrame({'state' : state, 'cl' : cl, 'area':area, 'pop':pop})


Then I did the grouping:

data_1 = data.groupby(['state', 'cl', 'area'])['pop'].median()


How to sign the resulting values ​​in the previous dataframe by the values ​​of the 'state', 'cl', 'area' columns?

I've already broken my head...
Data_1.keys() has already sorted and collected a tuple in a DataFrame... but I feel that the task is solved much easier

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2021-07-30
@dmshar

It looks like you did everything to confuse those who try to help you.
Let's start with the title of the question. How can panda s and seriae s be combined ?? Without even mentioning the unknown second of these terms - let's assume you meant Series - how can one UNION a Pandas module and an object of type Series ??
We go further.
We take your dataframe.

state           cl    area       pop
0   California     tropic  423967  38332521
1    Texas          tropic   695662  26448193
2    New York       norm  141297  19651127
3     Florida      tropic  170312  19552860
4    Illinois       norm  149995  12882135

If you grouped at least by cl - I would at least understand something. We would get two groups - tropical and temperate states, then we would look for the median of the population for each of these groups.
And what does a grouping mean, including not only the name of the state, but also the area and endowment ?? How many items in each group do you propose to get???
Move on.
How to sign the resulting values ​​in the previous dataframe by column values
​​What does "sign VALUES" mean??? And "by the values ​​(??) of the columns? A column can have many values. What and how are you going to sign?
(By the way, I hope you understand that your data_1 is NOT a DataFrame? )
If you did a grouping, about which I wrote above
data_1 = data.groupby(['cl'])['pop'].median()
then the result would indeed be an object of type Series
cl
norm      16266631
tropic    26448193

What are you going to sign here?
Going further:
data_1.keys() iterated and assembled a tuple into a DataFrame
What were you iterating over?
Clearly, data_1.keys() will return the multi-index of your new dataframe.
MultiIndex([('California', 'tropic', 423967),
            (   'Florida', 'tropic', 170312),
            (  'Illinois',   'norm', 149995),
            (  'New York',   'norm', 141297),
            (     'Texas', 'tropic', 695662)],
           names=['state', 'cl', 'area'])

What do you need?? And what tuple did you convert to DataFrame? And most importantly - why? An element of a multi-index can be accessed by the index of the list.
Well, the main question (obviously, since it is written in the title of the topic) - what are you combining with what? He remained a mystery.
In general, if you really need an answer - try to formulate the question in such a way that it would take less effort to find its solution than to solve those riddles that you cram into your question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question