精华内容
下载资源
问答
  • In R when you need to retrieve a column index based on the name of the column you could doidx Is there a way to do the same with pandas dataframes?解决方案Sure, you can use .get_loc():In [45]: df = ...

    In R when you need to retrieve a column index based on the name of the column you could do

    idx

    Is there a way to do the same with pandas dataframes?

    解决方案

    Sure, you can use .get_loc():

    In [45]: df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})

    In [46]: df.columns

    Out[46]: Index([apple, orange, pear], dtype=object)

    In [47]: df.columns.get_loc("pear")

    Out[47]: 2

    although to be honest I don't often need this myself. Usually access by name does what I want it to (df["pear"], df[["apple", "orange"]], or maybe df.columns.isin(["orange", "pear"])), although I can definitely see cases where you'd want the index number.

    展开全文
  • Dataframe df1包含具有列名的字段“Column headers”。我想创建另一个数据帧df2,它只包含df1的“column headers”列中的列标题。print(df1['Column header'])>>0 % Female1 ...

    Dataframe df1包含具有列名的字段“Column headers”。我想创建另一个数据帧df2,它只包含df1的“column headers”列中的列标题。print(df1['Column header'])

    >>

    0 % Female

    1 % Below poverty line

    2 % Rural population

    3 Decadal Population Growth (in %)

    4 Availability of Drinking Water Source Within P...

    5 Concrete Roofs (in %)

    6 Houses With Electricity (in %)

    7 Houses With Televisions (in %)

    8 With Computer/Laptop (in %)

    9 Houses With Phones (Telephone + Mobile) (in %)

    10 Houses With 2 wheelers (in %)

    11 Houses With cars (in %)

    12 Households With Banking Services (in %)

    13 Literacy Rate (in %)

    14 Literacy Rate (Rural) (in %)

    15 Literacy Rate (Urban) (in %)

    16 Decadal Difference In Literacy Rate

    17 Student: Teacher Ratio - All Schools

    18 Student: Teacher Ratio - Primary

    19 Student: Teacher Ratio - Upper Primary

    20 Under-five Mortality Rate (Per 1000 live Births)

    21 No of Dispensaries per 1,00,000 population

    22 No of Doctors per 1,00,000 population

    23 Total patients registered for tuberculosis tre...

    24 Sex Ratio (Females Per 1000 Males)

    25 Agri GSDP (%)

    26 Industry GSDP (%)

    27 Service GSDP (%)

    28 Unemployment Rate (2011-12)

    29 Rural Unemployment Rate (2011-12)

    30 Urban Unemployment Rate (2011-12)

    31 Per Capita Public Expenditure (in Rs)

    32 Per Capita Private Expenditure (in Rs)

    33 Infant Mortality Rate (IMR)

    34 Maternal Mortality Rate

    35 Coverage Of National Highways (Total in km)

    36 Coverage Of State Highways (Total in km)

    37 Coverage Of Rural Roads (Total in km)

    38 Coverage Of Urban Roads (Total in km)

    39 Railway Coverage (Total in km)

    40 Tele-Density [Total Connections / Total Popul...

    Name: Column headers, dtype: object

    我想创建包含40列的数据帧df2,如前所述。此数据框中的行将由另一个函数填充。

    我试图创建df2如下-df2 = pd.DataFrame() #Creating an empty dataframe

    df2.columns = df1['Column header']

    >>

    ValueError: Length mismatch: Expected axis has 0 elements, new values have 41 elements

    是否可以在Pandas中创建一个空白数据框,然后指定列名?

    展开全文
  • Python Excel 列名转换

    千次阅读 2013-05-08 13:05:57
    def colname_to_num(colname):  if type(colname) is not str:  return colname  col = 0  power = 1  print len(colname)  for i in xrange(len(colname) - 1, -1, -1):
    def colname_to_num(colname):
    
        if type(colname) is not str:
            return colname


        col = 0
        power  = 1
        print len(colname)
        for i in xrange(len(colname) - 1, -1, -1):
            ch = colname[i]
            print ch
            col += (ord(ch) - ord('A') +  1 ) * power
            power *= 26
        print col-1
        return col - 1




    def colnum_to_name(colnum):
        if type(colnum) != int:
            return colnum
        if colnum > 25:
            ch1 = chr(colnum % 26 + 65)
            ch2 = chr(colnum / 26 + 64)
            print ch2+ch1
            return ch2 + ch1
        else:
            print chr(colnum % 26 + 65)
            return chr(colnum % 26 + 65)




    if __name__ == "__main__":
        colname_to_num("CU")
        colnum_to_name(26)
    展开全文
  • DATAFRAME中,有这样一堆变量形式均为industry_行业名称,所以想请问一下如果我想批量引用这些列除了df[['industry_行业名称1','industry_行业名称2','……']]。有没有其他的批量引用方式哇?

    DATAFRAME中,有这样一堆变量形式均为industry_行业名称,所以想请问一下如果我想批量引用这些列除了df[['industry_行业名称1','industry_行业名称2','……']]。有没有其他的批量引用方式哇?

    展开全文
  • # 列名与列号字典,例如{'小区名称':2} self.col_dict = {} self.mismatchedcgi = dict() self.mismatchedcgi_t = dict() self.mismatchedrowid = [] # sheet.ncols: Nominal number of columns in sheet. ...
  • I have a bunch of column names in a Python list. Now I need to use that list as the column names in a SELECT statement. How can I do that?pythonlist = ['one', 'two', 'three']SELECT pythonlist FROM dat...
  • python——修改Dataframe列名的两种方法

    万次阅读 多人点赞 2017-06-02 20:51:01
    首先新建一个Dataframe import pandas as pd df = pd.DataFrame({'a':[1,2,3],'b':[1,2,3]}) 如下: a b 0 1 1 1 2 2 2 3 3 1、修改列名a,b为A、B。 df.columns = ['A','B'] ...2、只修改列名a为A
  • 主要介绍了python获取Pandas列名的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
  • # 方法1 a.columns = ['a','b','c'] # 方法2 a.rename(columns={'A':'a', 'B':'b', 'C':'c'}, inplace = True)
  • python pandas 修改列名

    2021-05-12 10:47:28
    https://www.jb51.net/article/137888.htm还有其他若干方法。
  • python pandas 读取列名以及index

    千次阅读 2019-11-24 17:39:05
    读取df的列名 [col for col in df] df.columns.values返回一个array,用tolist()返回一个list df.columns返回一个index,用tolist()返回一个list list(df) 读取index df.index.tolist() ...
  • pandas中DataFrame想根据列名读取数据,数据如下图 想获取feature_name: ['DVI', 'NDVI_B8A7', 'NDVI_B86']的数据 代码如下: #从excel中读取数据 dataset_path=r'C:\Users\Administrator\Desktop\test\...
  • python 修改dataframe列名的几种方法

    千次阅读 2021-01-25 14:08:20
    实际开发中经常有修改dataframe列名的需求,特意总结了如下可用的几种方法。 import pandas as pd def t1(): df = pd.DataFrame({'c1':[1, 2, 3], 'c2': [4, 5, 6]}) print(df) df.columns = ['d1', 'd2'] ...
  • python修改Dataframe列名的两种方法

    万次阅读 多人点赞 2020-03-26 17:29:04
    首先新建一个Dataframe import pandas as pd df = pd.DataFrame({'a':[1,2,3],'b':[1,2,3]}) ...1、修改列名a,b为A、B。 df.columns = ['A','B'] 2、只修改列名a为A df.rename(columns={'a':'A'}) ...
  • python dataframe 获得 列名columns 和行名称 index

    万次阅读 多人点赞 2018-09-25 18:38:40
    dfname._stat_axis.values.tolist() # 行名称 dfname.columns.values.tolist() # 列名称  
  • 作图时左侧列名显示不全 如图所示: 问题描述: 使用了网上说的如下代码问题未解决 plt.tight_layout() 解决方案: 利用函数subplots_adjust() 在该官方文档中可以看到,subplots_adjust()函数的作用是调整子图...
  • 【转】Python生成csv列名乱码

    千次阅读 2018-07-13 19:38:01
    解决方法:pandas的DataFrame to_csv方法实现csv文件输出,但是遇到中文乱码问题,已验证的正确的方法是:df.to_csv("cnn_predict_result.csv"...)python csv 格式文件 中文乱码问题解决方法...
  • [ [(b,1.0)], [(d,1.0)], [(b,1.0),(c,1.0)], [(a,1.0),(b,1.0)] ] I want to use gensim of python library which requires corpus as this form. Is there any smart way to do this or to apply gensim from ...
  • python pandas 修改列名,索引名

    千次阅读 2020-09-24 21:36:24
    第一,重命名 a = pd.DataFame(np.arange(12).reshape(3,4),columns=[‘a’,‘b’,‘c’,‘d’]) a.columns = [‘a1’,‘b1’,‘c1’,‘d1’] 第二,引入字典 (rename + 字典 ) a.rename(columns = {‘a’:‘a1’},...
  • [ [(b,1.0)], [(d,1.0)], [(b,1.0),(c,1.0)], [(a,1.0),(b,1.0)] ] I want to use gensim of python library which requires corpus as this form. Is there any smart way to do this or to apply gensim from ...
  • def fromCSVGetROI(CSVROIFileName): roi = pd.read_csv(CSVROIFileName,header=None) return roi

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 38,190
精华内容 15,276
关键字:

python没有列名

python 订阅