简体   繁体   中英

concatenate rows in Pandas.read_csv

What could have been the issue here? I'm trying to concatenate row[1] and row[2] to give something like eg DavidJoe and same goes with row[3] and row[4]

import pandas as pd
import datetime
#import pandas.io.data
from pandas_datareader import data, wb
import csv
import time


df = pd.read_csv('testfile.csv')
df = [[row[0],row[1] + row[2],row[3] + row[4], row[5],row[6]] for row in df.iterates()]

print (df)

Error:

Error: from pandas import to_datetime
ImportError: cannot import name 'to_datetime'

You look into http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.cat.html

import pandas as pd

df = pd.read_csv('testfile.csv')
for row in df.iterrows():
    print row[1].str.cat(sep=',')

Pandas doesn't know how to interpret the line where you define df :

df = [[row[0], row[1] + row[2], row[3] + row[4], row[5], row[6]] for row in df.iterates()]

Do you mean to collapse a 7-row csv into a 4-row dataframe? You can brute-force construct it using .loc[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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