简体   繁体   中英

Read csv and split single column to multiple

I'm new to python. I use python to read a csv file. The data appears to be like multiple columns, but when I try to manipulate the data, it is actually one single column. How can I separate them into multiple columns as dataframe? I used df = pd.read_csv('disease_data.csv', engine='python', sep=',') to read it.

在此处输入图像描述

enter image description here

the data is like this, a sequence of integers 25577, 7, 14, 5, 7, 8

25581, 0, 0, 4, 5, 6

25585, 0, 8, 9, 3, 5

25591, 0, 0, 0, 2, 5

I am unsure if this will help you but this is How I read a CSV File that was for a School Project

with open('members.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            # Index 0 - Forename, Index 1 - Surname, Index 2 - Category, Index 3 -Password 
            forenames.append(row[0])
            surnames.append(row[1])
            categories.append(row[2])
            passwords.append(row[3])

This used the module CSV import csv

Here is the CSV

Angela,Rich,Adult,Twinkle$
Siraj,Adkins,Junior,Password123%
Stefano,Love,Senior,Qwerty#
Cameron,Wilder,Junior,Wildtime$
Griff,Sutherland,Adult,Geordieman2%
Amaan,Sosa,Senior,Mollymoo5#
Isaak,Schroeder,Junior,Christmas#
Nana,Galloway,Junior,TheGrinch$
Lila,Blanchard,Adult,Tigger27$
Eren,Acosta,Adult,Jamjar1992#

Here is a link to the pandas.read_csv() docs.

Use example:

import pandas as pd
df = pd.read_csv('/path/to/file.csv')

It seems you need to convert that single column from a df object to a pd.Series object - using series_name = df1.iloc[0] where 0 is the header for the only column we have. Be back with more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM