English 中文(简体)
从安达的一组数据重新命名栏目
原标题:Renaming column names from a data set in pandas

我试图从一个名称有空间的数据框架中改名。 数据框架(df)由45栏组成,多数按名称有空间。 例如:df.column. Values 页: 1 日期:。 I Trial DataFrame.rename ( and DataFrame.columns. Values[] but do not work. 如果你能够帮助我发现我错失的东西,我将不胜感激。

for colmns in df:
    if     in colmns:
        colmns_new =  _ .join(colmns.split())
        df = df.rename (columns = {""%s"" %colmns : ""%s"" %colmns_new})   
    else:
        print (colmns)    

print (df)

或:

for i in range (len(df.columns)):
    old= df.columns.values[i]
    if     in old:
        new =  _ .join(old.split())
        df = df.columns.values[i] = [ %s  % new]
        print (""%s"" % new) 
print (df)

页: 1 附录 rror: 单项标注没有特性栏目

最佳回答
import pandas as pd
df.columns = [i.replace(   , _ ) for i in df.columns]
问题回答

您仅可提供<条码>df.columns = df.columns.str.replace( ,__),以强调取代该空间。

这里就是一个例子。 页: 1 但<代码>b 2和c 3有一空间。

>>> df = pd.DataFrame({ a1 : range(1,5),  b 2 : list ( abcd ),  c 3 :list( pqrs )})
>>> df
   a1 b 2 c 3
0   1   a   p
1   2   b   q
2   3   c   r
3   4   d   s
>>> df.columns = df.columns.str.replace(   , _ )
>>> df
   a1 b_2 c_3
0   1   a   p
1   2   b   q
2   3   c   r
3   4   d   s

您可使用雷管取代空间,强调

这方面的一个例子是,有些栏目含有空间,

cols = [ col {} .format(i) for i in range(1, 10, 1)] + [ col10 ]
df = pd.DataFrame(columns = cols)

import re
df.columns = [re.sub(   , _ ,i) for i in df.columns]

页: 1

col_1   col_2   col_3   col_4   col_5   col_6   col_7   col_8   col_9   col10




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签