Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

pandas - Python: Creating a function that extracts a range of rows from a dataset

I would like to create a function that extracts a range of rows (n:m) from a dataset (.csv). It should ignore the first row, also, it should convert in integer the "Number Minutes" column.

Our dataset ("file_name.csv") would look like this:

?Name;Age;Team;Number Minutes
Henry Dalton;19;Chelsea FC;1.200
John Smith;50;Manchester Utd.;4.000
Herman Lock;32;Manchester City;2.400
Kyle Hard;23;Everton Football Club;1.200

The function would look like this:

 find_player("file_name.csv", 2, 3)

And the outcome would be:

['John Smith' , '50' , 'Manchester Utd.' '4000']
['Herman Lock' , '32' , 'Manchester City' , '2400']

I've chose to use pandas. So far I've made this code:

import pandas as pd
file_route = r'.file_name.csv'

def find_player(file_route, n, m):
    data = pd.read.csv(file_route)
    data2 = data[n, m].copy()
    data2[3] = data2[3].astype(int)
    print(data2)
    return(data2)

I can't get the correct solution, so I could use some help.

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
def find_player(filename,start,end):
    with open(filename, 'r') as msg:
        data = msg.read().splitlines() 
    return [[int(s.replace(".","")) if s.replace(".","").isdigit() else s for s in item.split(";")] for item in data[start:end+1]]

print (find_player("file_name.csv", 2, 3))

Output:

[['John Smith', 50, 'Manchester Utd.', 4000], ['Herman Lock', 32, 'Manchester City', 2400]]

Please note than you can't really get two lists as shown in your example. Either you get a list of lists, either you run your function once per line you want to catch.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...