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

andy
  • 55
  • 6

2 Answers2

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}]}
SeaBean
  • 22,547
  • 3
  • 13
  • 25
  • 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 Jun 30 '21 at 21:09
  • @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 ? – SeaBean Jun 30 '21 at 21:29
  • @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 ? – SeaBean Jun 30 '21 at 21:33
  • @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. – SeaBean Jun 30 '21 at 21:37
  • @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. – SeaBean Jun 30 '21 at 21:46
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

0sharp
  • 43
  • 6