0

I have el next dataframe

from pandas import *
from numpy import *


data=read_csv('enero.csv')
data



           Fecha           DirViento  MagViento  
0   2011/07/01  00:00        318        6.6      
1   2011/07/01  00:15        342        5.5        
2   2011/07/01  00:30        329        6.6        
3   2011/07/01  00:45        279        7.5        
4   2011/07/01  01:00        318        6.0        
5   2011/07/01  01:15        329        7.1        
6   2011/07/01  01:30        300        4.7        
7   2011/07/01  01:45        291        3.1   

How to split the column Fecha in two columns,for example, get a dataframe as follows:

       Fecha     Hora     DirViento  MagViento  
0   2011/07/01  00:00        318        6.6      
1   2011/07/01  00:15        342        5.5        
2   2011/07/01  00:30        329        6.6        
3   2011/07/01  00:45        279        7.5        
4   2011/07/01  01:00        318        6.0        
5   2011/07/01  01:15        329        7.1        
6   2011/07/01  01:30        300        4.7        
7   2011/07/01  01:45        291        3.1 
3
Contributors
3
Replies
45
Views
4 Years
Discussion Span
Last Post by vegaseat
0

You first have to be able to access each individual row and know what format the row is in; string, list, etc. We do not know this so any code would be shooting in the dark.

0

If the csv file contains the time data, then you might be better off to split date and time in the csv file.

0

Example on how to modify a CSV file ...

# data string from typical csv file
# header removed
csv_data = '''\
2011/07/01  00:00,318,6.6
2011/07/01  00:15,342,5.5
2011/07/01  00:30,329,6.6 
2011/07/01  00:45,279,7.5'''

mylist = csv_data.split('\n')
#print(mylist)  # test

newlist = []
for line in mylist:
    line_list = line.split(',')
    #print(line_list)  # test
    #print(line_list[0])  # test
    #print(line_list[0].split())  # test
    date, time = line_list[0].split()
    newlist.append([date, time, line_list[1], line_list[2]])

#print(newlist)  # test
csv_data_new = ""
for item in newlist:
    #print(','.join(item))  # test
    csv_data_new += ','.join(item) + '\n'

print(csv_data)
print('-'*30)
print(csv_data_new)

# optionally add header string
header = "Fecha,Hora,DirViento,MagViento\n"
csv_data_new = header + csv_data_new

fname = "new_data.csv"
with open(fname, "w") as fout:
    fout.write(csv_data_new)

''' result ...
2011/07/01  00:00,318,6.6
2011/07/01  00:15,342,5.5
2011/07/01  00:30,329,6.6 
2011/07/01  00:45,279,7.5
------------------------------
2011/07/01,00:00,318,6.6
2011/07/01,00:15,342,5.5
2011/07/01,00:30,329,6.6 
2011/07/01,00:45,279,7.5
'''

Now you can read the modified CSV file into pandas ...

''' pandas_read_csv101.py

file new_data.csv content ...
Fecha,Hora,DirViento,MagViento
2011/07/01,00:00,318,6.6
2011/07/01,00:15,342,5.5
2011/07/01,00:30,329,6.6 
2011/07/01,00:45,279,7.5

used Windows installer 
pandas-0.12.0.win32-py3.3.exe
'''

import pandas as pd

data = pd.read_csv('new_data.csv')

print(data)

''' result ...
        Fecha   Hora  DirViento  MagViento
0  2011/07/01  00:00        318        6.6
1  2011/07/01  00:15        342        5.5
2  2011/07/01  00:30        329        6.6
3  2011/07/01  00:45        279        7.5
'''
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.