简体   繁体   English

我想在逗号后一一打印一列的单元格值

[英]I want to print cell value of a column one by one after comma

import pandas as pd


data = {'product_name': ['laptop,computer', 'printer,table', 'tablet,mobile', 'desk', 'chair,table'],
        'price': [1200, 150, 300, 450, 200]
        }

df_merge = pd.DataFrame(data)

I want to print data by using for loop 1st it should come laptop then computer then printer and so on.我想通过使用 for loop 1st 打印数据,它应该是笔记本电脑,然后是计算机,然后是打印机等等。 for example- enter image description here例如 -在此处输入图像描述

I tried below lines of code but not getting what I want.我尝试了下面的代码行,但没有得到我想要的。

for i in (df_merge.product_name):
print(i)

USING FOR LOOP Code:使用 FOR 循环代码:

for i in (df_merge.product_name):
    for s in i.split(','):
        print(s)

Output; Output;

laptop
computer
printer
table
tablet
mobile
desk
chair
table

You can use .str.split(',') and then .explode() :您可以使用.str.split(',')然后.explode()

print(*df_merge["product_name"].str.split(",").explode(), sep="\n")

Prints:印刷:

laptop
computer
printer
table
tablet
mobile
desk
chair
table

If you want to explode whole dataframe:如果要整个dataframe:

df_merge["product_name"] = df_merge["product_name"].str.split(",")
df_merge = df_merge.explode("product_name")
print(df_merge)

Prints:印刷:

  product_name  price
0       laptop   1200
0     computer   1200
1      printer    150
1        table    150
2       tablet    300
2       mobile    300
3         desk    450
4        chair    200
4        table    200

暂无
暂无

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

相关问题 Python/Pandas:使用一个列的值作为我想要一个值的列名的后缀 - Python/Pandas: use one column's value to be the suffix of the column name from which I want a value 我只想在 kivy 中一个接一个地单击两个按钮时打印文本 - I want to print a text only if the two buttons are clicked one after the other in kivy 我如何在检测到一个“0”后打印一个“end”值并恢复到检测“1” - How do i print one value of 'end' after detecting one '0' and revert back into detecting '1's 我想按一列中的重复项对数据框进行排序 - I want to sort dataframe by duplicates in one column 我想替换一列中的 NaN 值,但我的另一列是条件 - I want to replace NaN value in one column, but my other column is condition 如何根据另一列打印一列的值 - How to print value from one column based on another column 我想返回两个值,但只打印一个 - i want to return two values, but only print one 根据另一列的值提取一个单元格的值 - Pandas / Python - Extracting the value of one cell conditionning on the value of another column - Pandas / Python 如何在第 2 列中移动一个单元格,以便我的日期位于第 0 行,而其他所有值都保留? - How do I shift one cell in column 2 so that my date is at row 0 while everything other value remains? 我如何根据列单元格值和 append 查找一个 dataframe 上的一行到另一个 dataframe 上的一行? - How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM