3

I just got a csv file that I want to load the dataset as dataframe using pandas. However, I'm kinda confused this data format.

Here is the sample of data for two lines:

Name=John, Gender=M, BloodType=A, Location=New York, Age=18
Name=Mary, Gender=F, BloodType=AB, Location=Seatle, Age=30

How do I load this dataset into dataframe with columns (Name, Gender, BloodType,...etc)?

I will appreciate that someone gives me tips to start with!

3
  • Use .read_csv() Commented May 15, 2019 at 4:05
  • Yup, I do use pandas.read_csv(). Yet, it will make every column with this format 'attribute=value'. Commented May 15, 2019 at 4:16
  • Sorry, misunderstood. Commented May 15, 2019 at 4:22

2 Answers 2

5

Use the pandas read_csv method to read the csv file. Here is a sample program how you can do that:

import pandas as pd

data = pd.read_csv("path_to_csv_file")

print(data)
Sign up to request clarification or add additional context in comments.

Comments

3

Use read_csv with header=None first:

import pandas as pd

temp=u"""Name=John,Gender=M,BloodType=A,Location=New York,Age=18
Name=Mary,Gender=F,BloodType=AB,Location=Seatle,Age=30"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), header=None)
print (df)
           0         1             2                  3       4
0  Name=John  Gender=M   BloodType=A  Location=New York  Age=18
1  Name=Mary  Gender=F  BloodType=AB    Location=Seatle  Age=30

Then DataFrame.apply with Series.str.split and select second lists, last change columns names:

df1 = df.apply(lambda x: x.str.split('=').str[1])
df1.columns = df.iloc[0].str.split('=').str[0].rename(None)
#if necessary
df1['Age'] = df1['Age'].astype(int)
print (df1)
   Name Gender BloodType  Location  Age
0  John      M         A  New York   18
1  Mary      F        AB    Seatle   30

1 Comment

I used the same way!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.