0

Assume that we have the following tuple

    S=({'country':[('India')],'state':[('Telangana')],'city':[('Hyderabad'),('Vizag')]}, {'date':[{'year': 2021, 'month':10}]})
    
    <class 'tuple'>
    

Can we convert this into a dataframe and get the date part into a variable

    df = key      value
         country  India
         state    Telangana
         city     Hyderabad
         city     Vizag
         
    Date = {'date':[{'year': 2021, 'month':10}]}

I have tried pd.DataFrame(json.loads(json.dumps(s))) but its not clean

1
  • I don't understand the need for a dataframe here...why not just S[1]? Commented Jun 30, 2021 at 20:27

2 Answers 2

1

For the first part, you can try:

pd.Series(S[0]).explode()

Result:

country    India    
state      Telangana
city       Hyderabad
city       Vizag    
dtype: object

If you need a dataframe, you can use:

df = pd.DataFrame(pd.Series(S[0]).explode().reset_index().values, columns=['key', 'value'])

Result:

print(df)

       key      value
0  country      India
1    state  Telangana
2     city  Hyderabad
3     city      Vizag

For the second part on Date, you can use:

Date = S[1]

Result:

print(Date)

{'date': [{'year': 2021, 'month': 10}]}
Sign up to request clarification or add additional context in comments.

5 Comments

looks good but the result dataframe has braces and commas in the value column key value 0 country ( India) 1 state (Telangana) 2 city (Hyderabad,) 3 city ( Vizag,) how do I remove braces and commas for the value column
@andy That's weird. I tested with your tuple S and arrived at the dataframe I shown in the result. No braces and comma there. Did you run the code with the tuple S given here, or run on another tuple ?
@andy Just by dumping S[0] on my system gives {'country': ['India'], 'state': ['Telangana'], 'city': ['Hyderabad', 'Vizag']} which already have no braces there. So, how come the braces come out afterwards ? Is the tuple that you run contains list of strings instead of list of tuple ?
@andy You can also test out using the code: df = pd.Series(S[0]).explode().to_frame(name='value').rename_axis(index='key').reset_index() and see how it goes.
@andy There is absolutely no reason to get comma in the value column together with the text. This could happen only when inside your tuple you have something like 'city':['(Hyderabad,)', ' (Vizag,)'] Note the placement of single quote character ' is different from that in S that you provided. There probably the braces are inside single quotes as a string. Please double check with your data.
0

For the Date part, you can try

Date = S[1]
print(Date)
{'date': [{'year': 2021, 'month': 10}]}

As for the first part of your question, you may find the following code less than perfect, but it can easily be edited to match the desired output perfectly.

df = pd.DataFrame(
    {k:pd.Series(v) for k, v in dict(S[0]).items()}
).transpose()
print(df)
               0      1
country      India    NaN
state    Telangana    NaN
city     Hyderabad  Vizag

For more information, refer to this post

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.