Python Forum
[solved] dataframe and read_csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] dataframe and read_csv
#1
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

Attached Files

.csv   minimal_data.csv (Size: 418 bytes / Downloads: 437)
Reply
#2
Hello,

I.m not an expert with pandas, however:
  • data is already a dataframe.
  • drop lines 10 - 13 and all will be fine.

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) 
Output:
( MyColumn1 MyColumn2 0 09.06.2021 14:35:05 100 1 09.06.2021 14:36:16 100 2 09.06.2021 14:37:26 100 3 09.06.2021 14:38:37 100 4 09.06.2021 14:39:48 100 5 09.06.2021 14:40:59 100 6 09.06.2021 14:42:10 100 7 09.06.2021 14:43:21 100 8 09.06.2021 14:44:32 100,)
Reply
#3
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'
Reply
#4
(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])
Output:
MyColumn1 datetime64[ns] MyColumn2 int64 dtype: object MyColumn1 MyColumn2 0 2021-09-06 14:35:05 178 1 2021-09-06 14:36:16 59 2 2021-09-06 14:37:26 0 3 2021-09-06 14:38:37 0 4 2021-09-06 14:39:48 0 5 2021-09-06 14:40:59 0 6 2021-09-06 14:42:10 0 7 2021-09-06 14:43:21 0 8 2021-09-06 14:44:32 0 ------------------------------ MyColumn1 MyColumn2 2 2021-09-06 14:37:26 0 3 2021-09-06 14:38:37 0 4 2021-09-06 14:39:48 0 5 2021-09-06 14:40:59 0 6 2021-09-06 14:42:10 0 7 2021-09-06 14:43:21 0 8 2021-09-06 14:44:32 0
ju21878436312 likes this post
Reply
#5
@snippsat: Thank you very much for the useful comments! Dance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Delete a line in a dataframe using .reset_index() and .drop() ju21878436312 2 5,745 Feb-25-2022, 02:55 PM
Last Post: ju21878436312
  read_csv error ilcaa72 2 4,493 May-29-2019, 02:58 PM
Last Post: ilcaa72
  Pandas Dataframe through read_csv() ift38375 1 3,140 May-29-2019, 05:56 AM
Last Post: buran

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.