0

I'm trying to download this test json file to use it in a classification project.I have the converted this JSON file to a csv file in pandas using this code:

test_sarcasm_df = pd.read_json('/content/drive/My Drive/Sarcasm Data/test_sarcasm.json')

I am using it in an ML project but it's in the wrong format. I'm not familiar with JSON and its a big file so is there any way I can format it where the first column becomes the first row in my dataframe?

I appreciate any help you guys can give me!

Here is what the data frame looks like: enter image description here

Qi'ra
  • 37
  • 6
  • 1
    are you saying to you need to transpose rows and columns? If so, this might help https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.transpose.html – bguest Nov 28 '21 at 02:18
  • @bguest Thank you! I think that will handle the columns but I forgot about the data? How do I move the values so that they will also appear under the right column? For example, Fishburn goes under author. – Qi'ra Nov 28 '21 at 02:27
  • It will be much easier if you post your data in a reusable format for other people and include specifics of what your desired output is. You can check out this Stack Overflow post on [How to make good, reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – scotscotmcc Nov 28 '21 at 02:46

1 Answers1

2

Pandas transpose solves this problem exactly:

from pandas import DataFrame


df = DataFrame([{"row1": "text2", "row2": "text1"},{"row1": "bob", "row2": "james"}])
df.index = ["text", "author"]
print(df.head(2))
# this is essentially the starting position
         row1   row2
text    text2  text1
author    bob  james



# now we transpose
df = df.transpose()
print(df.head(2))

# problem solved :)
       text author
row1  text2    bob
row2  text1  james
bguest
  • 215
  • 1
  • 7