unable to convert groupby dataset to json in pandas [duplicate] - pandas

This question already has answers here:
How to reset index in a pandas dataframe? [duplicate]
(3 answers)
Closed 4 years ago.
I have group by data set but I'm unable to convert it to json. It throws out json with a bad format. TO_excel works fine.
Country Sub amount
3 source4
UK 1 source3
1 source1
US 2 source2
How can I export groupby dataset to_json?

There is problem you have MultiIndex in DataFrame, so need reset_index:
j = df.reset_index().to_json()
print (j)
{"Country":{"0":"UK","1":"UK","2":"US"},
"Sub":{"0":1,"1":1,"2":2},
"amount":{"0":"source3","1":"source1","2":"source2"}}

Related

How can I get an expanded version of a dataframe which has lists as values in it? [duplicate]

This question already has answers here:
How to unnest (explode) a column in a pandas DataFrame, into multiple rows
(16 answers)
Closed 6 months ago.
How can I get an expanded version of a dataframe which has lists as values in it?
Here's a sample of the dataframe I have:
raw = pd.DataFrame().assign(Therapuetic_Area = ['Oncology'],
LocationState = [['Ohio','Illinois','Oregon','New York']])
Now, I need it to look like this edited DataFrame:
edited = pd.DataFrame().assign(Therapuetic_Area = ['Oncology','Oncology','Oncology','Oncology'],LocationState = ['Ohio','Illinois','Oregon','New York'])
Is there a Pandas method I can use for this? How could I get the edited dataframe without having to manually input the values? I can't possibly manually input it because my data is enormously large. Any help would be appreciated!
you can use explode to create rows from the list values
raw.explode('LocationState')
Therapuetic_Area LocationState
0 Oncology Ohio
0 Oncology Illinois
0 Oncology Oregon
0 Oncology New York

Merging on Index and rearranging columns of a pandas dataframe in Python [duplicate]

This question already has answers here:
How can I pivot a dataframe?
(5 answers)
Closed 2 years ago.
I am completely new to programming and started learning Python recently.
I have a pandas data frame df as shown in image 1 and trying to rearrange the columns as shown in image 2.
Can you please help me to complete this.
Thanks and Regards,
Arya.
You can use pd.pivot_table like this:
df=pd.DataFrame({'index':[0,1,2,0,1,2],'Name':['A','A','A','B','B','B'],'Value':[10,20,30,15,25,35]})
df.pivot_table(index='index',columns='Name',values='Value').reset_index()
Out[8]:
Name index A B
0 0 10 15
1 1 20 25
2 2 30 35

how do I split 1 str column into 2 columns in a pandas dataframe [duplicate]

This question already has answers here:
How to split a dataframe string column into two columns?
(11 answers)
Closed 2 years ago.
enter image description here
df['business location'] #column i want to split into 2 columns :
df['longitude'] #and
df['latitude']
df[['longitude','latitude']] = sf['Business Location'].str.split(',')
is giving me error:
ValueError: Must have equal len keys and value when setting with an iterable
how do I split?
This will work
df.assign(
longitude=lambda x: x['Business Location'].str.split(',').str[0],
latitude=lambda x: x['Business Location'].str.split(',').str[1])

Panda: Why the dataframe is not appended? [duplicate]

This question already has answers here:
Appending to an empty DataFrame in Pandas?
(5 answers)
Creating an empty Pandas DataFrame, and then filling it
(8 answers)
Closed 3 years ago.
I am trying to append a new row to an empty dataset and i found the below code fine:
import panda as pd
df = pd.DataFrame(columns=['A'])
for i in range(5):
df = df.append({'A': i}, ignore_index=True)
So, it gives me:
A
0 0
1 1
2 2
3 3
4 4
But, when i try the below code, my dataset is still empty:
df = pd.DataFrame(columns=['A'])
df.append({'A': 2}, ignore_index=True)
df
Can someone explain me the solution to add only 1 row?

How to convert ndarray to pandas DataFrame [duplicate]

This question already has answers here:
Convert two numpy array to dataframe
(3 answers)
Closed 3 years ago.
I have ndarray data with the shape of (231,31). now I want to convert this ndarray to pandas DataFrame with 31 columns. I am using this code:
for i in range (1,32):
dataset = pd.DataFrame({'Column{}'.format(i):data[:,i-1]})
but this code just creates the last column, it means with 231 indexes and just 1 column, but I need 31 columns. is there any way to fix this problem and why it happens?
Every time you are creating a new dataframe, that is why only the last column remains.
You need to create the dataframe with pd.DataFrame(data).