简体   繁体   English

使用 pandas.dataframe 格式化数据

[英]format data using pandas.dataframe

I am new to pandas, I need some guidance.我是熊猫的新手,我需要一些指导。 I have a dictionary and I wrote the code below to create a dataframe to write to excel sheet.我有一本字典,我编写了下面的代码来创建一个数据框来写入 Excel 工作表。

dict = { "name": "xyz",
         "desciption":"abc",
         "paths": ["a","b","c"]
       }
df = pd.DataFrame(dict)

............................................................... ………………………………………………………………………………………………………………………………………………………… ………………

I have to create a dataframe and write it to excel.我必须创建一个数据框并将其写入excel。 My excel should show data in the format:我的excel应该以以下格式显示数据:

name  description  paths
xyz   abc           a
                    b
                    c

but what i am actually getting is:但我实际得到的是:

name  description  paths
xyz   abc           a
xyz   abc           b
xyz   abc           c

You can find the duplicates of columns name and desciption and make them empty strings:您可以找到列namedesciption的重复项并将它们desciption空字符串:

# keep only the first one, mark others as duplicates
dups = df.duplicated(["name", "desciption"], keep="first")

# put empty string to those places
df.loc[dups, ["name", "desciption"]] = ""

which gives这使

>>> df

  name desciption paths
0  xyz        abc     a
1                     b
2                     c

and you can write this frame to file.您可以将此帧写入文件。

A solution might be as follows:解决方案可能如下:

import pandas as pd

paths = ["a", "b", "c"]

d = {"name": ["xyz"] + [''] * (len(paths) - 1),
     "desciption": ["abc"] + [''] * (len(paths) - 1),
     "paths": paths
     }
df = pd.DataFrame(d)

print(df)

  name desciption paths
0  xyz        abc     a
1                     b
2                     c

You can get rid of index numbers via df.to_excel(index=False) .您可以通过df.to_excel(index=False)摆脱索引号。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM