[solved] dataframe and read_csv - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [solved] dataframe and read_csv (/thread-33975.html) |
[solved] dataframe and read_csv - ju21878436312 - Jun-15-2021 Aim: I would like to convert data, read with read_csv and convert it to a dataframe. What I've tried: 1. data = pd.read_csv(...) 2. pd.DataFrame(data) Problem: The columns are not shown in the dataframe as expected in 2 columns. import pandas as pd import numpy as np from datetime import datetime, timedelta # read data, date_parser=[0]: first column to datetime, data = pd.read_csv('minimal_data.csv', delimiter = ';', date_parser=[0], usecols=[0, 1], header = 0, names = ["MyColumn1","MyColumn2"]), print(data) df = pd.DataFrame(data) print(df)--- MacOS 10.15.7, Jupyter notebook RE: dataframe and read_csv - Larz60+ - Jun-15-2021 Hello, I.m not an expert with pandas, however:
import pandas as pd import numpy as np from datetime import datetime, timedelta # read data, date_parser=[0]: first column to datetime, data = pd.read_csv('minimal_data.csv', delimiter = ';', date_parser=[0], usecols=[0, 1], header = 0, names = ["MyColumn1","MyColumn2"]), print(data)
RE: dataframe and read_csv - ju21878436312 - Jun-15-2021 Thank you. I actually need to further evaluate the data. And if I try it directly with data, I get: data.dtypes AttributeError: 'tuple' object has no attribute 'dtypes'or data.loc[data['MyColumn2'] == 0] AttributeError: 'tuple' object has no attribute 'iloc' RE: dataframe and read_csv - snippsat - Jun-15-2021 (Jun-15-2021, 01:26 PM)ju21878436312 Wrote: hank you. I actually need to further evaluate the data. And if I try it directly with data, I get:You need to get thee DataFrame out of tuple. Here a example with some advice. import pandas as pd import numpy as np # Pandas has own datateime do not need to use this #from datetime import datetime, timedelta # read data, date_parser=[0]: first column to datetime, data = pd.read_csv('minimal_data.csv', delimiter = ';', date_parser=[0], usecols=[0, 1], header=0, names=["MyColumn1","MyColumn2"]), # Get DataFrame out of tupe df = data[0] # Convert to datetime64 df['MyColumn1'] = pd.to_datetime(df['MyColumn1']) print(df.dtypes) print(df) print('-' * 30) print(df.loc[df['MyColumn2'] == 0])
RE: dataframe and read_csv - ju21878436312 - Jun-16-2021 @snippsat: Thank you very much for the useful comments! |