[PYTHON] Create a new csv with pandas based on the local csv

Create a new csv with pandas based on the local csv

Suppose the following csv is local.

sample.csv


id,age,name,money
111,11,sato,1100
222,22,suzuki,20000
333,33,takahashi,330000
444,44,tanaka,440000
555,55,ito,500000
666,66,yamamoto,800000
777,77,nakamura,1000000

Based on this, create a new csv that satisfies the following.

Execution environment

code

gencsv.py


import pandas as pd

id_list = []
age_list = []
name_list = []
money_list = []

read_csvfile = 'sample.csv'
def stack_data():
    global name_list
    global id_list
    global age_list 
    global money_list 

    #Read data from csv
    data = pd.read_csv(read_csvfile)
    #Store in each list
    for name in data['name']:
        name_list += [name]
    for i in data['id']:
        id_list += [i]
    for age in data['age']:
        age_list += [age]
    for money in data['money']:
        money_list += [money]


def generate_csv():
    #Substitute the acquired data in the data frame
    for i in range(len(name_list)):
        df = pd.DataFrame({
            'name': name_list[i],
            'age': age_list[i],
            'money': money_list[i],
        },index=[i,])

        csv_title =  'personID=' + str(id_list[i]) + '.csv'

        #Generate csv based on data frame
        df.to_csv(csv_title, index=False)
        print(csv_title+'Was generated')
    print("All created")


stack_data()
generate_csv()

Execution result

$ python3 gencsv.py
personID=111.csv was generated
personID=222.csv was generated
personID=333.csv was generated
personID=444.csv was generated
personID=555.csv was generated
personID=666.csv was generated
personID=777.csv was generated
All created

At the end

Recommended Posts

Create a new csv with pandas based on the local csv
Dynamically create new dataframes with pandas
Try to create a new command on linux
Create a new page in confluence with Python
Create a translation tool with the Translate Toolkit
Create a GUI on the terminal using curses
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
How to create a submenu with the [Blender] plugin
Create a QR code for the URL on Linux
Create a Todo app with the Django REST framework
Make a breakpoint on the c layer with python
Fill outliers with NaN based on quartiles in Pandas
Create a tweet heatmap with the Google Maps API
Make a CSV formatting tool with Python Pandas PyInstaller
[Python] A memo to write CSV vertically with Pandas
Create a shape on the trajectory of an object
When reading a csv file with read_csv of pandas, the first column becomes index
Read csv with python pandas
Create a local pypi repository
Create a bot that only returns the result of morphological analysis with MeCab on Discord
Create a local scope in Python without polluting the namespace
Create a list in Python with all followers on twitter
Probably the easiest way to create a pdf with Python3
Create a homepage with django
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a heatmap with pyqtgraph
Create a new list by combining duplicate elements in the list
Create a classroom on Jupyterhub
Create a directory with python
Control the motor with a motor driver using python on Raspberry Pi 3!
I tried cross-validation based on the grid search results with scikit-learn
Create a color picker for the color wheel with Python + Qt (PySide)
Create a word cloud with only positive / negative words on Twitter
Create a pandas Dataflame by searching the DB table using sqlalchemy
Is the new corona really a threat? Validated with Stan (was)
Format the CSV file of "National Holiday" of the Cabinet Office with pandas
Create a Docker container image with JRE8 / JDK8 on Amazon Linux
Create a REST API to operate dynamodb with the Django REST Framework
Create and return a CP932 CSV file for Excel with Chalice
Create a compatibility judgment program with the random module of python.
Extract the maximum value with pandas.
Create a CSV reader in Flask
Create a pandas Dataframe from a string.
Create a Python environment on Mac (2017/4)
Create a virtual environment with Python!
[Python] About creating a tool to create a new Outlook email based on the data of the JSON file and the part that got caught
Create a SlackBot service on Pepper
Create a Linux environment on Windows 10
Create a python environment on centos
Draw a graph with pandas + XlsxWriter
Create an age group with pandas
Create a poisson stepper with numpy.random
Create a file uploader with Django
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
Read and format a csv file mixed with comma tabs with Python pandas
Steps to create a Python virtual environment with VS Code on Windows
[Can be done in 10 minutes] Create a local website quickly with Django
Scrap the published csv with Github Action and publish it on Github Pages
Create a Python multi-user platform with JupyterHub + JupyterLab on Rapsberry Pi 3B +!
Install the python module with pip on a server without root privileges
[Python, ObsPy] I drew a beach ball on the map with Cartopy + ObsPy.