python - 在 pandas 中拆分和复制 csv 字段

标签 python pandas csv data-analysis

我有一个 csv 文件,例如

ID、名称、产品

101,乐购,苹果;香蕉;橙子

102,Lidl,果汁;酸奶

103,阿尔迪,水果;蔬菜;大米

使用 pandas 库,我想将它们拆分为一个新的 csv,这样对于产品列,每个字段只有一个值

我尝试过的以下代码仅选择那些特定列并将值写入新的 csv。我想修改输出。

df=pd.read_csv('final.csv',delimiter=",", index_col="ID",encoding="ISO- 
8859-1")
df1=df[,"Name","Products"]
df1.to_csv('a.csv',header='True')
print (df1)

我希望生成的新 csv 具有以下格式的值-

101、乐购、苹果

101,乐购,香蕉

101,乐购,橙子

102,Lidl,果汁

102,Lidl,酸奶

103,阿尔迪,水果

..

最佳答案

我们可以:

1) 用分隔符“;”分割每行中的项目。然后我们为每个项目获取一列。

2)然后,我们将列拆开,将它们作为行,然后删除基于先前列值添加的索引级别。

3) 将此 pd.Series 命名并加入主 df。

prod = df['products'].str.split(";", expand=True).unstack().reset_index(level=0, drop=True).dropna()

    ID
    101         Apple
    102         Juice
    103        Fruits
    101        Banana
    102        Yogurt
    103    vegetables
    101       Oranges
    103          rice

prod.name = 'product'

df = df.join(prod.to_frame())
df.drop('products', axis=1, inplace=True)

Output:

      Name     product
ID                    
101  Tesco       Apple
101  Tesco      Banana
101  Tesco     Oranges
102   Lidl       Juice
102   Lidl      Yogurt
103   Aldi      Fruits
103   Aldi  vegetables
103   Aldi        rice

关于python - 在 pandas 中拆分和复制 csv 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54738156/

相关文章:

python - 按字母顺序排序数据框的索引

python - Plotly 在 jupyter lab 中给出一个空字段作为输出

Python dict 如何在一行中创建 key 或更新 key ?

python - 如何读取文件并附加其内容?

python - 迭代 Excel 文件,向系列添加列,并使用 Panda 库保存结果

powershell - 在Powershell的2列中显示CSV文件

python - Django 多对多字段

python - 如何从数据框中创建项目字典?

python - 神经网络 - 类型错误 : can't multiply sequence by non-int of type 'float'

javascript - 单击按钮后下载 CSV 文件