简体   繁体   English

如何根据名称变量中的条件打印特定的 output

[英]How to print specific output based on condition from name variable

I have a dataframe data like that:我有一个像这样的 dataframe data

数据

ANd I would like an output like that:我想要一个像这样的 output :

  • name1名称1

    sentences1句子1

    sentences2句子2

    sentences3句子3

  • name2名称2

    sentences4句子4

  • name3名称3

    sentences5句子5

    sentences6句子6

Here's what I did so far:这是我到目前为止所做的:

first = True

for i in range(0, data.shape[0]):
    if data['Name'][i+1] == data['Name'][i] :
        if first:
            first = False
            print(data['Name'][i])
            print()
            print(data['Sentences'][i])
        else:
            print()
            print(data['Sentences'][i])
           
    if data['Name'][i] != data['Name'][i+1] :
        print(data['Name'][i])
        print(data['Sentences'][i])
       
    else :
        print()

But that doesn't give me the right output, the name aren't print at the place I wanted.但这并没有给我正确的 output,名字没有印在我想要的地方。 I think I misplace or forget something in my loop but what?我想我放错地方或忘记了循环中的某些东西,但是什么?

Thanks谢谢

EDIT: I think I figured it out, I would like to know if I'm right: that's what I change in my code :编辑:我想我明白了,我想知道我是否正确:这就是我在代码中所做的更改:

first = True

for i in range(0, data.shape[0]):
    if data['Name'][i+1] == data['Name'][i] :
        if first:
            first = False
            print(data['Name'][i])
            print()
            print(data['Sentences'][i])
        else:
            print()
            print(data['Sentences'][i])
           
    if data['Name'][i] != data['Name'][i+1] :
        print(data['Name'][i+1]) #i+1 instead of i
        print(data['Sentences'][i+1])# i+1 instead of i
       
    else :
        print()

I believe there's a much cleaner way of doing it.我相信有一种更清洁的方法。

Note:笔记:

My DataFrame looks like this:我的 DataFrame 看起来像这样:

在此处输入图像描述

Code代码

for name in df.name.unique():
    print(name + ':\n')
    for index, value in df.sentences[df.name == name].items():
        print(value)
    print('\n')

Output Output

name1:

sentence1
sentence2
sentence3


name2:

sentence4


name3:

sentence5
sentence6

Try to modify the code above to your needs and see if it helps.尝试根据您的需要修改上面的代码,看看是否有帮助。

暂无
暂无

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

相关问题 如何根据两个变量的条件打印特定的 output - How to print specific output based on condition from two variables 根据条件打印特定输出 - Print a certain output based on a condition 如何从基于3个列表的特定索引输出输出(所有3个列表在相同位置上) - How do I print out an output from a specific index based upon 3 Lists (same position on list for all 3) 如何从 python 中的 *args 打印变量名 - how to print variable name from *args in python 循环以根据名称与行内容相同的条件创建具有特定文件名和内容的输出文件 - loop to create output files with specific file names and content based on condition where name will be same as row content 如何在打印语句或任何文本 output 语句中打印变量名称。 Python,Jupyter 笔记本? - How to print a variable name in a print statement or any text output statement. Python, Jupyter Notebook? 如何根据 Python 中的 if 语句条件将打印命令输出重定向到文件或屏幕? - How to redirect print command output either to a file or to screen based on an if statement condition in Python? 如何从 API output 打印特定值? - How do I print a specific value from API output? 根据Python数据框中的变量打印特定行 - Print specific row based on variable in Python dataframe 如何从列表中提取数据帧的名称并将其作为标题打印到输出? - How to extract the name of a dataframe from a list and print it as a heading to the output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM