Skip to content

Instantly share code, notes, and snippets.

@kantale
Created December 19, 2017 09:18
Show Gist options
  • Select an option

  • Save kantale/92437d2f99aa2e3cd4c611ea18827427 to your computer and use it in GitHub Desktop.

Select an option

Save kantale/92437d2f99aa2e3cd4c611ea18827427 to your computer and use it in GitHub Desktop.
Εισαγωγή στον προγραμματισμό με τη γλώσσα python 2017-2018, διάλεξη 9η
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kantale
Copy link
Copy Markdown
Author

kantale commented Jan 29, 2018

Όσοι κάνετε την άσκηση με pandas (που είναι μια καλή ιδέα) η παρακάτω εντολή μετατρέπει το column DATE από string σε Date format

data['DATE'] = pd.to_datetime(data['DATE'], format='%Y-%m-%d')

@kantale
Copy link
Copy Markdown
Author

kantale commented Feb 3, 2018

Παρατηρούμε ότι το INITIAL SAMPLE SIZE περιέχει text από το οποίο πρέπει να "εξάγουμε" κάποιο νούμερο. Μπορείτε να χρησιμοποιήσετε αυτή τη μέθοδο για να το κάνετε αυτό:

import re
import pandas as pd

def convert(x):
    # Get the first number
    s = re.search(r'[\d,\.]', x)
    if not s:
        print ('Warning. Could not find a number at the field:', x)
        return 0
    
    #Remove commas
    found = s.group(0).replace(',', '').replace('.', '')
    if not found:
        print ('Warning: Could not parse:', x)
        return 0
    return int(found)

df = pd.read_csv('gwas_catalogue.tsv', sep='\t', converters = {
    'INITIAL SAMPLE SIZE': convert})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment