Home > Blockchain >  How to get CSV odd and even lines in Python?
How to get CSV odd and even lines in Python?

Time:10-01

I'm converting R script to Python.

My actual Python code:

table_NameUrl = pd.read_csv("Data.txt", sep=";", header= None)
table_Size = len(table_NameUrl)
sequence = np.arange(1, table_Size, 2)
for i in sequence:
    print("Table name: "   str(table_NameUrl[[1]][i]))
    print("URL: "   str(table_NameUrl[[1]][i   1]))
    tableName = table_NameUrl[[1]][i]
    tableUrl = table_NameUrl[[1]][i   1]
    fileName = (tableName   ".csv")
    ro.globalenv['fileName'] = fileName
    last_url = "/n3/all/n6/in n3 28"
    url = (tableUrl   last_url)
    ro.globalenv['url'] = url
    ro.r('table= get_sidra(api= url)')
    ro.r('write.csv(table, file= fileName, row.names = F)')

In R, I can get CSV odd and even lines with: table_NameUrl[[1]][i] and table_NameUrl[[1]][i 1]

But in Python [[1]][i] and [[1]][i 1] it didn't work.

The error is:

"None of [Int64Index([1], dtype='int64')] are in the [columns]"

Short version of CSV:

Panu 2.1.1, 2.1.5

/t/2093/p/2000,2010/v/93/c86/2776,2777,2778,2779,2780,2781/c2/0,4,5/c1/1,2/

Panu 5.1.1, 5.1.2, 5.1.3, 5.1.4, 5.1.5, 5.1.6, 5.1.7, 5.1.9, 5.1.10

/t/1612/p/2013,2014,2015,2016,2017/v/109,216,214/c81/2688,2691,2692,2694,2696,2708,2702,2715,2703/

Panu 5.1.8

/t/839/p/2013,2014,2015,2016,2017/v/109,216,214/c81/114254/

In R, the output of tableName = table_NameUrl[[1]][i] is the name of table, e.g.:

Panu 2.1.1, 2.1.5

And the output of tableUrl = table_NameUrl[[1]][i 1] is the url, e.g.: /t/2093/p/2000,2010/v/93/c86/2776,2777,2778,2779,2780,2781/c2/0,4,5/c1/1,2/

Can someone help me to convert this function?

Thanks in advance!

CodePudding user response:

As per the docs, you can use the iloc function to select rows in a dataframe:

import pandas as pd 

table_NameUrl = pd.read_csv("Data.txt", sep=";", header= None)
even_rows = table_NameUrl.iloc[::2]
odd_rows = table_NameUrl.iloc[1::2]

OR

even_rows = table_NameUrl.iloc[lambda x: x.index % 2 == 0]
odd_rows = table_NameUrl.iloc[lambda x: x.index % 2 == 1]

CodePudding user response:

You can use these two functions to get the even or odd rows of a dataframe:

def get_even_rows(dataframe):
    return dataframe[[i % 2 == 0 for i in range(df.shape[0])]]


def get_odd_rows(dataframe):
    return dataframe[[i % 2 != 0 for i in range(df.shape[0])]]
  • Related