Home > OS >  How to split a dataframe with multiple values in the same row, with strings
How to split a dataframe with multiple values in the same row, with strings

Time:07-19

I read my pandas dataframe as:

ERA5RS12 = pd.read_csv('F:/ERA5_RS/[12]-PIRATA-2017_20171127_100021/wspd_hl.csv')

ERA5RS12

My output is

I want some like this:

Latitude,Longitude,Value,Level

9,33,8.4,20

9,33,10,40

9,33,11,100

CodePudding user response:

If I were you, I would write my own reader to read data like this. With the following code, you can have your column names in header list, and your table rows in lines list.

with open('test.csv', 'r') as file:
    
    header = []
    lines = []
    
    for i, line in enumerate(file):
        
        if i == 0:
            header = line.split()
            continue
            
        if i % 2 == 0:
            continue
            
        lines.append(line.split())

When the data are processed, you can easily create pd.DataFrame, and clean the data.

df = pd.DataFrame(lines, columns=header)
df['Value'] = df.Value.str.strip(',').astype(float)

The result is following.

>> df.head()
  Latitude Longitude   Value level
0    9.000   321.990  6.6732    20
1    9.000   321.990  6.8282    30
2    9.000   321.990  6.9814    50
3    9.000   321.990  7.0756    70
4    9.000   321.990  7.1705   100

CodePudding user response:

Ensure that the delimiter of your CSV file is ,. Otherwise, you can specify a custom delimiter corresponding to the file with parameter sep while calling pd.read_csv. see a documentation.

Edited: According to the Excel file you provided, I convert it to CSV format. Here is a snippet to produce what you need.

import pandas as pd

with open("wspd_hl.csv", "r") as f:
    lines = [line.strip() for line in f.readlines()]

df = pd.DataFrame(columns=lines[0].split(" "))
for i, line in enumerate(lines[1:], 1):
    if i % 2 == 1:
        line = line.replace(",", "")
        df.loc[len(df)] = [value for value in line.split(" ") if value.strip() != ""]

Output:

index Latitude Longitude Value level
0 9.000 321.990 6.6732 20
1 9.000 321.990 6.8282 30
2 9.000 321.990 6.9814 50
3 9.000 321.990 7.0756 70
4 9.000 321.990 7.1705 100
5 9.000 321.990 7.2190 120
6 9.000 321.990 7.2613 140
7 9.000 321.990 7.2989 160
8 9.000 321.990 7.3352 180
9 9.000 321.990 7.3685 200
10 9.000 321.990 7.4524 250
11 9.000 321.990 7.5457 300
12 9.000 321.990 7.6750 350
13 9.000 321.990 7.9962 400
14 9.000 321.990 8.3183 450
15 9.000 321.990 8.4191 500
16 9.000 321.990 8.3831 550
17 9.000 321.990 8.2993 600
18 9.000 321.990 8.3508 650
19 9.000 321.990 8.4924 700
20 9.000 321.990 8.6865 750
21 9.000 321.990 8.9590 800
22 9.000 321.990 9.2247 850
23 9.000 321.990 9.4725 900
24 9.000 321.990 9.6867 950
25 9.000 321.990 9.8466 1000
26 9.000 321.990 10.1283 1100
27 9.000 321.990 10.4210 1200
28 9.000 321.990 10.7595 1300
29 9.000 321.990 11.1359 1400
30 9.000 321.990 11.5150 1500
31 9.000 321.990 11.8761 1600
32 9.000 321.990 12.5596 1800
33 9.000 321.990 12.8827 1900
34 9.000 321.990 13.2004 2000
35 9.000 321.990 13.5117 2200
36 9.000 321.990 13.3459 2400
37 9.000 321.990 12.7713 2600
38 9.000 321.990 11.9837 2800
39 9.000 321.990 11.1624 3000
  • Related