Shamoon
Shamoon

Reputation: 43539

How can I remove the first element from a list with pandas.read_csv?

I have:

X = pd.read_csv(
    "data/train.csv", header=0, usecols=['Type', 'Age', 'Breed1', 'Breed2', 'Gender', 'Color1', 'Color2', 'Color3', 'MaturitySize', 'FurLength',    'Vaccinated',   'Dewormed', 'Sterilized',   'Health',   'Quantity', 'Fee', 'VideoAmt', 'PhotoAmt'])
Y = pd.read_csv(
    "data/train.csv", header=0, usecols=['AdoptionSpeed'])
print(Y)

This gives:

       AdoptionSpeed
0                  2
1                  0
2                  3
3                  2
4                  2
5                  2
6                  1
7                  3

I assume the first column is the index, and the second is the AdoptionSpeed. I want to then map over the values, but when I do something like:

Y = map(lambda y: float(y) / 4, Y)

I get an error:

ValueError: could not convert string to float: AdoptionSpeed

So how do I remove that first row? Or better yet - is there a better way to map?

Upvotes: 2

Views: 76

Answers (3)

BENY
BENY

Reputation: 323316

More like

df.AdoptionSpeed.map(lambda x : x/4)
Out[52]: 
0    0.50
1    0.00
2    0.75
3    0.50
4    0.50
5    0.50
6    0.25
7    0.75
Name: AdoptionSpeed, dtype: float64

Upvotes: 1

hunzter
hunzter

Reputation: 598

When working with pandas, do not use map like this. Use column wise operation. Or pandas's apply

Something like this for divide:

# cast type
Y['AdoptionSpeed'] = Y['AdoptionSpeed'].astype(float)

# devide by 4, assign to a new columns
Y['AdoptionSpeed_4'] = Y['AdoptionSpeed'] / 4

# or apply
Y['AdoptionSpeed_4'] = Y['AdoptionSpeed'].apply(lambda v: v / 4)

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71590

Use:

Y = map(lambda y: float(y) / 4, Y['AdoptionSpeed'].tolist())

To make it work.

Even better:

Y = Y.apply(lambda y: float(y) / 4)

Upvotes: 3

Related Questions