Pandas 将变量名传递给列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35278692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:39:44  来源:igfitidea点击:

Pandas Passing Variable Names into Column Name

pythonpandas

提问by JJSmith

I have a dataframe that contains 13 different column names, I have separated these headings into two lists. I now want to perform different operations on each of these lists.

我有一个包含 13 个不同列名的数据框,我将这些标题分成两个列表。我现在想对这些列表中的每一个执行不同的操作。

Is it possible to pass column names into pandas as a variable? My code at the moment can loop through the list fine but i am having trouble trying to pass the column name into the function

是否可以将列名作为变量传递给Pandas?我现在的代码可以很好地循环遍历列表,但是我在尝试将列名传递给函数时遇到了问题

Code

代码

CONT = ['age','fnlwgt','capital-gain','capital-loss']
#loops through columns
for column_name, column in df.transpose().iterrows():
    if column_name in CONT:
        X = column_name
        print(df.X.count())
    else:
        print('')

采纳答案by jezrael

I think you can use subsetcreated from listCONT:

我认为您可以使用subset创建于listCONT

print df
  age fnlwgt  capital-gain
0   a    9th             5
1   b    9th             6
2   c    8th             3

CONT = ['age','fnlwgt']

print df[CONT]
  age fnlwgt
0   a    9th
1   b    9th
2   c    8th

print df[CONT].count()
age       3
fnlwgt    3
dtype: int64

print df[['capital-gain']]
   capital-gain
0             5
1             6
2             3

Maybe better as listis dictionary, which is created by to_dict:

也许更好,因为listdictionary,这是由创建to_dict

d = df[CONT].count().to_dict()
print d
{'age': 3, 'fnlwgt': 3}
print d['age']
3
print d['fnlwgt']
3

回答by aiguofer

try:

尝试:

for column_name, column in df.transpose().iterrows(): 
    if column_name in CONT:
        print(df[column_name].count()) 
    else: 
        print('')

edit:

编辑:

To answer your question more precisely: You can use variables to select cols in 2 ways: df[list_of_columns]will return a DataFrame with the subset of cols in list_of_columns. df[column_name]will return the Series for column_name

更准确地回答您的问题:您可以使用变量以两种方式选择 cols:df[list_of_columns]将返回一个带有 cols 子集的 DataFrame in list_of_columnsdf[column_name]将返回系列column_name

回答by Alexander

The following will print the count of each column in the dataframe if it is a subset of your CONT list.

如果它是您的 CONT 列表的子集,以下将打印数据框中每列的计数。

CONT = ['age', 'fnlwgt', 'capital-gain', 'capital-loss']
df = pd.DataFrame(np.random.rand(5, 2), columns=CONT[:2])

>>> df
        age    fnlwgt
0  0.079796  0.736956
1  0.120187  0.778335
2  0.698782  0.691850
3  0.421074  0.369500
4  0.125983  0.454247

Select the subset of columns and perform a transform.

选择列的子集并执行转换。

>>> df[[c for c in CONT if c in df]].count()
age       5
fnlwgt    5
dtype: int64