Organising Csv. File Data in Python

Organising Csv. File Data in Python

I am quite a beginner with Python but I have a programming-related project to work on, so, I really would like to ask some help. I didn´t find many simple solutions to organize the data such a way that I could do some analysis with that. First, I have multiple csv-files, which I read in as DataFrame objects. In the end, I need them all to analyze together (right now the files are separated to the list of DataFrames but later on I probably will need those as one DataFrame object). However, I have a problem with organizing and separating the data. These are thousands of rows in one column, a part of it is presented:

                                            CIP;Date;Hour;Cons;REAL/ESTIMATED
EN025140855608477018TC2L;11/03/2020;1;0                                 057;R
EN025140855608477018TC2L;11/03/2020;2;0                                 078;R
EN025140855608477018TC2L;11/03/2020;3;0                                 033;R
EN025140855608477018TC2L;11/03/2020;4;0                                 085;R
EN025140855608477018TC2L;11/03/2020;5;0                                 019;R
                                                                    ...
EN025140855608477018TC2L;11/04/2020;20;0                                786;R
EN025140855608477018TC2L;11/04/2020;21;0                                288;R
EN025140855608477018TC2L;11/04/2020;22;0                                198;R
EN025140855608477018TC2L;11/04/2020;23;0                                728;R
EN025140855608477018TC2L;11/04/2020;24;0                                275;R

The area, where the huge space between, the number should be merged together, for example, 0.057, which information represents "Cons" (actually it is the most important information). I should be able to split data into 5 columns in order to proceed with the analysis. However, it should be a universal tool for different csv-files without knowing the including symbols. But the structure of the content and the heading is always the same.

I would be happy if anyone might know to recommend a way to work with this kind of data.

1 Answer

Sounds like what you are trying to do is convert the Cons column so that the spaces become a dot.

df = pd.read_csv("file.txt", sep=";")
df['Cons'] = df['Cons'].str.replace("\s+",".")
df['Cons'].head()

Output:

0    0.057
1    0.078
2    0.033
3    0.085
4    0.019

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.