Python3 - convert csv to json using pandas

2024/11/8 11:42:14

I've got a .csv files with 5 columns but I only need the json file to contain 3 of these how would i go about doing it?

csv file:

Ncode   Ocode   name    a     b     c 1      1.1     1x     1a    1b    1c2      2.2     2x     2a    2b    2c3      3.3     3x     3a    3b    3c

Json output:

{"1.1":[{"a":"1a"},{"b":"1b"},{"c":"1c"}],"2.2":[{"a":"2a"},{"b":"2b"},{"c":"2c"}]}
Answer
txt = """Ncode   Ocode   name    a     b     c 1      1.1     1x     1a    1b    1c2      2.2     2x     2a    2b    2c3      3.3     3x     3a    3b    3c
"""df = pd.read_csv(StringIO(txt), delim_whitespace=True)json.dumps({'{:0.2f}'.format(r.Ocode): [{'a': r.a}, {'b': r.b}, {'c': r.c}]for r in df.itertuples()}
)'{"2.20": [{"a": "2a"}, {"b": "2b"}, {"c": "2c"}], "3.30": [{"a": "3a"}, {"b": "3b"}, {"c": "3c"}], "1.10": [{"a": "1a"}, {"b": "1b"}, {"c": "1c"}]}'
https://en.xdnf.cn/q/120617.html

Related Q&A

Python List of Dictionaries by Loops

I have 2 python list of dictionaries:[{index:1,color:red},{index:2,color:blue},{index:3,color:green} ]and[{device:1,name:x},{device:2,name:y},{device:3,name:z} ]How can I append each dictionary from th…

Removing parentheses and comma

Im importing Data from a database into python data frame. Now, I wish to use the data for further analysis, however, I need to do a little cleaning of the data before using. Currently, the required col…

Explicit Exception problem with try function

I have to run codes irrespective whether it fails or not. Im using ExplicitException. Following is my code:try:G.add_edge(data1[0][0],data1[1][0],weight=data1[2+i][0]) except ExplicitException:passtry:…

How do I perform a bubble sort in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

check if number is between row of numpy array

Want to check if value is between the row of array. here the value 347 is in between the 1st row of aa but not second , so how to check for it ? aa= np.array([[348 ,345],[460 , 459 ]])value = 347prin…

Print a word diagonally? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Click a button using Selenium and Python

I have the following code: <a class="sectionname" href="#" onclick="expandAll();return false;">Expand all</a> When I click on expand all, the whole page loads…

how to do Json format [duplicate]

This question already has answers here:How to store ner result in json/ database(2 answers)Closed 8 years ago.(S(PERSON Rami/NNP Eid/NNP)is/VBZstudying/VBGat/IN(ORGANIZATION Stony/NNP Brook/NNP Univers…

python numpy arange dtpye? why converting to integer was zero

x = np.arange(0.3, 12.5, 0.6)print(x)[ 0.3 0.9 1.5 2.1 2.7 3.3 3.9 4.5 5.1 5.7 6.3 6.9 7.5 8.1 8.7 9.3 9.9 10.5 11.1 11.7 12.3]x = np.arange(0.3, 12.5, 0.6,int)print…

Where is my syntax error?

Im trying to see where a Python syntax error would be hiding. Both Django and pylint claim a syntax error at custom.py:41. Lines 41-42 read:(reading_threshold =int(request.POST[reading_threshold]))I do…