Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何迭代数据帧中的选定列,df?_Python_Pandas - Fatal编程技术网

Python 如何迭代数据帧中的选定列,df?

Python 如何迭代数据帧中的选定列,df?,python,pandas,Python,Pandas,我有一个数据帧,df有3775行×8列 我的df.columns是 Index(['FY', 'Month', 'Sales Area', 'BSP Agent', 'City Booking Office','General Sales Agent', 'Web Sales', 'Total'] of dtype='object' 对于列df[['BSP Agent','City Booking Office','General Sales Agent','Web Sales','Total

我有一个数据帧,df有3775行×8列

我的
df.columns

Index(['FY', 'Month', 'Sales Area', 'BSP Agent', 'City Booking Office','General Sales Agent', 'Web Sales', 'Total'] of dtype='object'
对于列
df[['BSP Agent','City Booking Office','General Sales Agent','Web Sales','Total']]
,我要执行以下操作:从列中删除空格,然后将对象转换为数字

我使用了一个
for循环
,但它非常混乱

for e in df_c:
    df_c[e] = df_c[e].replace(' ', '', regex=True)
    df_c[e] = pd.to_numeric(df_c[e], errors='coerce').fillna(0, downcast='infer')
    break

有更好的方法来完成我的任务吗?

您可以通过执行以下操作来避免for循环:

c = ['FY', 'Month', 'Sales Area', 'BSP Agent', 'City Booking Office',
     'General Sales Agent', 'Web Sales', 'Total']

df[c] = df[c].apply(lambda x: x.str.replace(' ',  ''))
df[c]= df[c].apply(pd.to_numeric, errors = 'coerce').fillna(0, downcast='infer')

您可以通过执行以下操作来避免for循环:

c = ['FY', 'Month', 'Sales Area', 'BSP Agent', 'City Booking Office',
     'General Sales Agent', 'Web Sales', 'Total']

df[c] = df[c].apply(lambda x: x.str.replace(' ',  ''))
df[c]= df[c].apply(pd.to_numeric, errors = 'coerce').fillna(0, downcast='infer')

如果“从文件创建数据帧”和“空间”适用于数千,最好的解决方案是在中使用
数千
参数,然后将正确的列转换为数字:

df = pd.read_csv(file, thousands=' ')

按列表将和用于所有选定列,并用于转换为数字:


样本

np.random.seed(123)

c =['FY', 'Month', 'Sales Area', 'BSP Agent', 
     'City Booking Office','General Sales Agent', 'Web Sales', 'Total']
cols = ['BSP Agent', 'City Booking Office', 'General Sales Agent', 'Web Sales', 'Total']

df = (pd.DataFrame(np.random.rand(5, len(cols)) * 10000, columns=cols)
         .astype(int)
         .applymap(lambda x: '{:,}'.format(x).replace(',', ' '))
         .reindex(c, axis=1, fill_value='data'))
print (df)
     FY Month Sales Area BSP Agent City Booking Office General Sales Agent  \
0  data  data       data     6 964               2 861               2 268   
1  data  data       data     4 231               9 807               6 848   
2  data  data       data     3 431               7 290               4 385   
3  data  data       data     7 379               1 824               1 754   
4  data  data       data     6 344               8 494               7 244   

  Web Sales  Total  
0     5 513  7 194  
1     4 809  3 921  
2       596  3 980  
3     5 315  5 318  
4     6 110  7 224  


如果“从文件创建数据帧”和“空间”适用于数千,最好的解决方案是在中使用
数千
参数,然后将正确的列转换为数字:

df = pd.read_csv(file, thousands=' ')

按列表将和用于所有选定列,并用于转换为数字:


样本

np.random.seed(123)

c =['FY', 'Month', 'Sales Area', 'BSP Agent', 
     'City Booking Office','General Sales Agent', 'Web Sales', 'Total']
cols = ['BSP Agent', 'City Booking Office', 'General Sales Agent', 'Web Sales', 'Total']

df = (pd.DataFrame(np.random.rand(5, len(cols)) * 10000, columns=cols)
         .astype(int)
         .applymap(lambda x: '{:,}'.format(x).replace(',', ' '))
         .reindex(c, axis=1, fill_value='data'))
print (df)
     FY Month Sales Area BSP Agent City Booking Office General Sales Agent  \
0  data  data       data     6 964               2 861               2 268   
1  data  data       data     4 231               9 807               6 848   
2  data  data       data     3 431               7 290               4 385   
3  data  data       data     7 379               1 824               1 754   
4  data  data       data     6 344               8 494               7 244   

  Web Sales  Total  
0     5 513  7 194  
1     4 809  3 921  
2       596  3 980  
3     5 315  5 318  
4     6 110  7 224  


为什么没有任何条件就有
break
语句?是否要从列名或列值中删除空格?我只想删除列值中的空格。我在那里停了一下,同时怀疑这可能是一个无限循环。但是我可以看出这是不必要的,因为没有任何条件可以满足。@jezrael,我已经纠正了它为什么你有
break
语句而没有任何条件?是否要从列名或列值中删除空格?我只想删除列值中的空格。我在那里停了一下,同时怀疑这可能是一个无限循环。但是我可以看出这是不必要的,因为没有条件可以满足。@jezrael,我已经更正了它确实
df[cols]。replace(“”,,,regex=True)
work for substring@jez?是的,我只是用一个例子来检查,它确实适用于substring@jez thanksDoes
df[cols].replace(“”,,,regex=True)
work for substring@jez?是的,我刚刚用一个例子检查了一下,它确实适用于substring@杰兹,谢谢