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

Friday, August 26, 2022

[FIXED] How to export dataframe column wise to separate csv files? And how to append the columns from different dataframe to the separated csv files

 August 26, 2022     csv, dataframe, pandas, python, python-3.x     No comments   

Issue

df1 is this:

    accel_name numb frequency resistance phase
idx                                           
3            K    0       -33          1  4030
4            K    1       -16          2  4028
5            K    2       -18         12  4036
6            K    3       -14         -3  4054
12           K    4        -2         17  4048
13           K    5       -18         12  4048

df2 is this:

    accel_name numb frequency resistance phase
idx                                           
7            P    0      -452       4089   329
8            P    1      -428       4082   427
9            P    2      -382       4078   518
10           P    3      -363       4052   545
11           P    4      -347       4064   508
14           P    5      -373       4068   409

output :

  1. append df2 columns to df1
  2. output frequency.csv, resistance.csv, phase.csv

Solution

You can use df.to_csv() to export the relevant dataframes

import os
import pandas as pd

filepath = os.getcwd()
df = pd.concat([df1, df2], axis=0).sort_index()
cols = ['frequency', 'resistance', 'phase']
for col in cols:
    df[[col]].to_csv(filepath + '\\' + col + '.csv')

print(df)
    accel_name numb frequency resistance phase
idx                                           
3            K    0       -33          1  4030
4            K    1       -16          2  4028
5            K    2       -18         12  4036
6            K    3       -14         -3  4054
7            P    0      -452       4089   329
8            P    1      -428       4082   427
9            P    2      -382       4078   518
10           P    3      -363       4052   545
11           P    4      -347       4064   508
12           K    4        -2         17  4048
13           K    5       -18         12  4048
14           P    5      -373       4068   409

Output: frequency.csv, phase.csv, resistance.csv

enter image description here enter image description here enter image description here



Answered By - perpetualstudent
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • 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