The Manthis
The Manthis

Reputation: 97

pandas remove spaces from Series

The question is, how to gain access to the strings inside of the first column so that string manipulations can be performed with each value. For example remove spaces in front of each string.

import pandas as pd
data = pd.read_csv("adult.csv", sep='\t', index_col=0)
series = data['workclass'].value_counts()
print(series)

enter image description here

Here is the file: Zipped csv file

Upvotes: 0

Views: 210

Answers (1)

jezrael
jezrael

Reputation: 863291

It is index, so use str.strip with series.index:

series.index = series.index.str.strip()

But if need convert series here to 2 columns DataFrame use:

df = series.rename_axis('a').reset_index(name='b')

Upvotes: 1

Related Questions