pandas - Removing space in dataframe python -
i getting error in code because tried make dataframe calling element csv. have 2 columns call file: companyname , qualityissue. there 3 types of quality issues: equipment quality, user, , neither. run problems trying make dataframe df.equipment quality, doesn't work because there space there. want take equipment quality original file , replace space underscore.
input:
top calling customers, equipment quality, user, neither, customer 3, 2, 2, 0, customer 1, 0, 2, 1, customer 2, 0, 1, 0, customer 4, 0, 1, 0,
here code:
import numpy np import pandas pd import pandas.util.testing tm; tm.n = 3 # data. data = pd.dataframe.from_csv('mydata.csv') # group data calling companyname , qualityissue columns. byqualityissue = data.groupby(["companyname", "qualityissue"]).size() # make pandas dataframe of grouped data. df = pd.dataframe(byqualityissue) # change formatting of data match want spiderplot read. formatted = df.unstack(level=-1)[0] # replace nan values zero. formatted[np.isnan(formatted)] = 0 includingtotals = pd.concat([formatted,pd.dataframe(formatted.sum(axis=1), columns=['total'])], axis=1) sortedtotal = includingtotals.sort_index(by=['total'], ascending=[false]) sortedtotal.to_csv('byqualityissue.csv')
this seems asked question , tried lots of solutions didn't seem work. here tried:
with open('byqualityissue.csv', 'r') f: reader = csv.reader(f, delimiter=',', quoting=csv.quote_none) return [[x.strip() x in row] row in reader] sentence.replace(" ", "_")
and
sortedtotal['qualityissue'] = sortedtotal['qualityissue'].map(lambda x: x.rstrip(' '))
and thought promising here http://pandas.pydata.org/pandas-docs/stable/text.html:
formatted.columns = formatted.columns.str.strip().str.replace(' ', '_')
but got error: attributeerror: 'index' object has no attribute 'str'
thanks in advance!
try:
formatted.columns = [x.strip().replace(' ', '_') x in formatted.columns]
Comments
Post a Comment