Python Pandas 打印出每个单元格的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33814887/
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-08-19 14:02:44  来源:igfitidea点击:

Python Pandas printing out values of each cells

pythonpandas

提问by Joseph Seung Jae Dollar

I am trying to fetch values from an excel file using pandas dataframe and print out the values of each cells. Im using read_excel()to populate the dataframe, and I am looking for specific rows using the following line of code:

我正在尝试使用 Pandas 数据框从 excel 文件中获取值并打印出每个单元格的值。我read_excel()用来填充数据框,我正在使用以下代码行查找特定行:

df.loc[df['Parcel_ID'] == parcel]

parcel being an arbitrary input from the user. And I simply use this to print out the cells:

包裹是来自用户的任意输入。我只是用它来打印单元格:

row = df.loc[df['Parcel_ID'] == parcel]

print row['Category']
print row['Sub_Category']
.
.
(more values)

What I want is only the values from the cells, yet I get dtypes, names of the column, and other junks that I don't want to see. How would I only print out the value from each cells?

我想要的只是来自单元格的值,但我得到了 dtypes、列的名称以及我不想看到的其他垃圾。我如何只打印出每个单元格的值?

采纳答案by Anton Protopopov

If you have several values in your row you could use following:

如果您的行中有多个值,您可以使用以下内容:

row['Category'].values.tolist()
row['Sub_Category'].values.tolist()

回答by EdChum

IIUC the following should work:

IIUC 以下应该工作:

print row['Category'][0]
print row['Sub_Category'][0]

what is returned will be a Series in your case a Series with a single element which you index into to return a scalar value

返回的内容将是一个系列,在您的情况下是一个带有单个元素的系列,您可以将其索引以返回标量值

回答by Karam Qusai

How to find a value in a column (Column 1 name) by another value in another column (Column 2 name)

如何通过另一列中的另一个值(列 2 名称)查找列(列 1 名称)中的值

    df = pd.read_excel('file.xlsm','SheetName') #Get a excel file sheet

Then you have two ways to get that:

那么你有两种方法可以得到它:

First Way:

第一种方式:

    Value_Target = df.loc[df['Column1name']== 'Value_Key']['Column2name'].values

Second Way:

第二种方式:

    Value_Target = df['Column1name'][df['Column2name']=='Value_Key'].values[0]

Value_Key is the value in a column you have Value_Target is the value you want to find using Value_Key

Value_Key 是您拥有的列中的值 Value_Target 是您要使用 Value_Key 查找的值