简体   繁体   English

从 csv 文件中分离出多行的 Column,并在 Python 中对它们进行排序

[英]Seperate a Column with multiple lines from a csv file and order them in Python

I have a csv file where I need to find the best Team according to their best 11 Players with Python. My Problem is the Column with the Player Name and the Team is in the same Box separated with 1 l empty line.我有一个 csv 文件,我需要根据 Python 的最佳 11 名球员找到最佳球队。我的问题是带有球员姓名的列和球队在同一个框中,用 1 l 空行分隔。 If I open the.csv file it looks like this:如果我打开 .csv 文件,它看起来像这样:

,Rating_x,Player,Rating_y,
8.32,"Didier Drogba

Chelsea, 42, FW",7.55

How can I create this.csv into this:我怎样才能创建 this.csv 到这个:

,Rating_x,Player,Team,Age,Position,Rating_y,
8.32,Didier Drogba,Chelsea, 42, FW,7.55

So I can order them to their teams and make a General Ranking.所以我可以将他们命令到他们的团队并进行综合排名。

I guess you want a comma-separated list to be converted into cells in a column?我想您希望将逗号分隔的列表转换为列中的单元格? Follow the steps below (From: https://www.adinstruments.com/support/knowledge-base/how-can-comma-separated-list-be-converted-cells-column-lt )按照以下步骤操作(来自: https://www.adinstruments.com/support/knowledge-base/how-can-comma-separated-list-be-converted-cells-column-lt

  1. Highlight the column that contains your list.突出显示包含您的列表的列。
  2. Go to Data > Text to Columns. Go 到数据 > 文本到列。
  3. Choose Delimited.选择定界。 Click Next.点击下一步。
  4. Choose Comma.选择逗号。 Click Next.点击下一步。
  5. Choose General or Text, whichever you prefer.选择您喜欢的常规或文本。
  6. Leave Destination as is, or choose another column.保留 Destination 不变,或选择另一列。 Click Finish.单击完成。

With python:使用 python:

series.str.split(',', expand=True)

I have done this by doing:我这样做是这样做的:

import pandas as pd
enter code here
df[['Name','TAP']] = df.Player.str.split("\n\n", expand=True)
del df['Unnamed: 0']
del df['Player']
df[['Team','AGE','POS']] = df.TAP.str.split(", ", expand=True)
del df['TAP']

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

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