python删除数据_删除数据框中的空间python

I am getting an error in my code because I tried to make a dataframe by calling an element from a csv. I have two columns I call from a file: CompanyName and QualityIssue. There are three types of Quality issues: Equipment Quality, User, and Neither. I run into problems trying to make a dataframe df.Equipment Quality, which obviously doesn't work because there is a space there. I want to take Equipment Quality from the original file and replace the space with an 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 is my code:

import numpy as np

import pandas as pd

import pandas.util.testing as tm; tm.N = 3

# Get the data.

data = pd.DataFrame.from_csv('MYDATA.csv')

# Group the data by calling CompanyName and QualityIssue columns.

byqualityissue = data.groupby(["CompanyName", "QualityIssue"]).size()

# Make a pandas dataframe of the grouped data.

df = pd.DataFrame(byqualityissue)

# Change the formatting of the data to match what I want SpiderPlot to read.

formatted = df.unstack(level=-1)[0]

# Replace NaN values with 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 to be a frequently asked question and I tried lots of the solutions but they didn't seem to work. Here is what I tried:

with open('byqualityissue.csv', 'r') as f:

reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE)

return [[x.strip() for x in row] for row in reader]

sentence.replace(" ", "_")

And

sortedtotal['QualityIssue'] = sortedtotal['QualityIssue'].map(lambda x: x.rstrip(' '))

formatted.columns = formatted.columns.str.strip().str.replace(' ', '_')

but I got this error: AttributeError: 'Index' object has no attribute 'str'

Thanks for your help in advance!

解决方案

Try:

formatted.columns = [x.strip().replace(' ', '_') for x in formatted.columns]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值