简体   繁体   English

如何在Pandas DataFrame行上打印列名?

[英]How to print the column name on Pandas DataFrame row?

I have this DataFrame: 我有这个DataFrame:

 df = pd.DataFrame({'day':['1/1/2017','1/2/2017','1/3/2017','1/4/2017','1/5/2017','1/6/2017','1/7/2017'],
                     'event':['Rain','Sunny','Snow','Snow','Rain','Sunny','Sunny'],
                   'temperature': [32, 35, 28,24,32,31,''],'windspeed':[6,7,2,7,4,2,'']})
 df

在此处输入图片说明

I am trying to find the headers for the missing values on index 6: 我试图找到索引6上缺少值的标头:

for x in df.loc[6]:
if x == '':
    print(df.columns.values)
else: print(x)

在此处输入图片说明

I have tried searching and the closest I could get was what I have now. 我已经尝试过搜索,而我能得到的最接近的就是我现在拥有的。 Ultimately I'm trying insert these values into the dataframe: temperature = 34, windspeed = 8. 最终,我尝试将这些值插入数据帧:温度= 34,风速= 8。

But my first step was simply trying to build the loop/if statement that says if x=='' & [COLUMN_NAME] == 'temperature'... and that is where I got stuck. 但是我的第一步只是尝试构建loop / if语句,该语句表示x ==''&[COLUMN_NAME] =='temperature'...,这就是我遇到的问题。 I'm new to python, just trying to learn Pandas. 我是python的新手,只是想学习Pandas。 I need to only return the column I'm on, and not a list of all the columns. 我只需要返回我所在的列,而不是所有列的列表。

There are better ways to do this, but this works. 有更好的方法可以做到这一点,但这是可行的。

for col, val in df.loc[6].iteritems():
    if not val: # this is the same as saying "if val == '':"
        print(col)
    else:
        print(val)

Modified from your code: 从您的代码修改:

for i,x in enumerate(df.loc[6]):
    if x == '':
        print(df.columns[i])
    else: print(x)

I would use list comprehension as follows: 我将使用列表推导,如下所示:

listOfNulls = [ind  for ind in df.loc[6].index if df.loc[6][ind] == '']

and when I print the listOfNulls , I get: 当我打印listOfNulls ,我得到:

>>>> print(listOfNulls)
Out: ['temperature', 'windspeed']

The key here is it understand that df.loc[6] is a pandas Series which has indices. 这里的关键是要理解df.loc [6]是具有索引的熊猫Series We are using the values of the Series to get the indices. 我们正在使用Series的值来获取索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打印pandas dataframe每一行的索引值、列名和列数据? - How to print index value, column name, and column data for each row of pandas dataframe? 如何获取pandas DataFrame中第二大行值的列名 - How to get column name for second largest row value in pandas DataFrame Pandas 数据框按索引选择行,按名称选择列 - Pandas dataframe select row by index and column by name 将第一行pandas数据帧转换为列名 - Convert first row of pandas dataframe to column name 如何打印 pandas DataFrame 的特定行? - How to print a specific row of a pandas DataFrame? Pandas DataFrame:如何水平打印单行? - Pandas DataFrame: How to print single row horizontally? 如何打印 Dataframe 的名称并检查 Python 中 Dataframe 中的行和列? - How can I print the name of Dataframe and check row and column in Dataframe in Python? 如何使用和打印 pandas dataframe 名称 - How to use and print the pandas dataframe name 如何从 pandas dataframe 中的当前行中减去前一行以创建一个新列,以每个名称重新启动进程? - How to subtract previous row from current row in a pandas dataframe to create a new column restarting the process with each name? 如何在带有误差线的条形图中绘制熊猫数据框的特定行和列(基于行名和列名)? - How to plot specific rows and columns of pandas dataframe (based on name of row and name of column) in bar plot with error bars?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM