PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 27, 2022

[FIXED] How to parse a csv stored as a pandas Series?

 August 27, 2022     csv, dataframe, pandas, python     No comments   

Issue

I have the following DataFrame(from a csv) :

df = pd.DataFrame(
    {
        "date": ["2022-08-01T12:21:05", "2022-08-01T12:21:12", "2022-08-01T12:21:05"],
        "content": [
            "1659356463,1.245050,0.000000",
            "1659356479,1.245050,0.000000",
            "1659356494,1.245050,0.000000",
        ],
    }
)

What is the best way to parse df['content'] as a DataFrame (to later output a new csv file) ?
The final DataFrame should look like this:

final_df = pd.DataFrame(
   {
       "timestamp": ["1659356463", "1659356479", "1659356494"],
       "tilt": ["1.245050", "1.246782", "1.230922"],
       "threshold": ["0.000000", "0.000000", "1.000000"],
   }
)

Solution

Try:

final_df = df.content.str.split(',', expand=True)
final_df.columns = ['timestamp', 'tilt', 'threshold']


Answered By - Nuri Taş
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing