Home > Back-end >  How to parse a csv stored as a pandas Series?
How to parse a csv stored as a pandas Series?

Time:08-17

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"],
   }
)

CodePudding user response:

Try:

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