简体   繁体   English

我需要使用 python 将 csv 文件中的一列拆分为两列

[英]I need to split one column in csv file into two columns using python

Hello everyone I am learning python I am new I have a column in a csv file with this example of value:大家好,我正在学习 python 我是新手 我在 csv 文件中有一个列,其中包含以下值示例: 在此处输入图像描述

I want to divide the column programme based on that semi column into two columns for example例如,我想将基于该半列的列程序分为两列

program 1: H2020-EU.3.1.
program 2: H2020-EU.3.1.7.

This is what I wrote initially这是我最初写的

import csv
import os
with open('IMI.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)
    
    with open('new_IMI.csv', 'w') as new_file:
              csv_writer = csv.writer(new_file, delimiter='\t')
              
    #for line in csv_reader:
       # csv_writer.writerow(line)

please note that after i do the split of columns I need to write the file again as a csv and save it to my computer请注意,在我进行列拆分后,我需要再次将文件写入 csv 并将其保存到我的计算机

Please guide me请指导我

Using .loc to iterate through each row of a dataframe is somewhat inefficient.使用.loc遍历 dataframe 的每一行效率有点低。 Better to split an entire column, with the expand=True to assign to the new columns.最好拆分整个列,将expand=True分配给新列。 Also as stated, easy to use pandas here:也如上所述,易于使用pandas在这里:

Code:代码:

import pandas as pd

df = pd.read_csv('IMI.csv')
df[['programme1','programme2']] = df['programme'].str.split(';', expand=True)
df.drop(['programme'], axis=1, inplace=True)

df.to_csv('IMI.csv', index=False)

Example of output: output 示例:

Before:前:

print(df)
       id     acronym  status                   programme           topics
0  945358  BIGPICTURE  SIGNED  H2020-EU.3.1.;H2020-EU3.1.7  IMI2-2019-18-01
1  821362      EBiSC2  SIGNED  H2020-EU.3.1.;H2020-EU3.1.7  IMI2-2017-13-06
2  116026     HARMONY  SIGNED                H202-EU.3.1.  IMI2-2015-06-04

After:后:

print(df)
       id     acronym  status           topics    programme1     programme2
0  945358  BIGPICTURE  SIGNED  IMI2-2019-18-01  H2020-EU.3.1.  H2020-EU3.1.7
1  821362      EBiSC2  SIGNED  IMI2-2017-13-06  H2020-EU.3.1.  H2020-EU3.1.7
2  116026     HARMONY  SIGNED  IMI2-2015-06-04  H2020-EU.3.1.           None

You can use pandas library instead of csv .您可以使用pandas库代替csv

import pandas as pd
df = pd.read_csv('IMI.csv')
p1 = {}
p2 = {}

for i in range(len(df)):
    if ';' in df['programme'].loc[i]:
        p1[df['id'].loc[i]] = df['programme'].loc[i].split(';')[0]
        p2[df['id'].loc[i]] = df['programme'].loc[i].split(';')[1]

df['programme1'] = df['id'].map(p1)
df['programme2'] = df['id'].map(p2)

and if you want to delete programme column:如果要删除programme列:

df.drop('programme', axis=1)

To save new csv file:要保存新的 csv 文件:

df.to_csv('new_file.csv', inplace=True)

暂无
暂无

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

相关问题 使用python和pandas将时间戳列拆分为CSV中的两个新列 - Split timestamp column into two new columns in CSV using python and pandas 使用python,我需要根据CSV文件中两列中的两个键取平均值 - Using python, I need to average value based on two keys in two columns from a CSV file 如何使用 pandas 或 numpy(python)将文本文件中的 integer 值从一列拆分为两列 - how to split an integer value from one column to two columns in text file using pandas or numpy (python) 使用 Python 3.8 将一个 CSV 文件中的一列(向量)与另一个 CSV 文件中的两列(向量和数组)进行比较 - Compare one column (vector) from one CSV file with two columns (vector and array) from another CSV file using Python 3.8 如何在python中将一列拆分为两列? - How to split one column into two columns in python? 使用 python pandas 将一列拆分为两列 - Split one column into two columns with python pandas 使用Python拆分CSV文件中的列的值 - Split values for columns in CSV file using Python 使用 python 中的一个 csv 文件比较一列中的两个数据 - compare two data in a columns using one csv file in python 使用 pandas 将.dat 文件转换为 csv 格式时出现问题,需要将 1 列拆分为多列 - Problem in converting .dat file into csv format using pandas, Need to split 1 column into multiple columns 将python文件中的数据分组时,将CSV文件中的一列拆分为多列(无熊猫) - Split one column in CSV file into multiple columns while grouping the data in Python (without Pandas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM