Categories
Python Answers

How to write a Pandas DataFrame to CSV file with Python?

Sometimes, we want to write a pandas DataFrame to CSV file with Python.

In this article, we’ll look at how to write a pandas DataFrame to CSV file with Python.

How to write a pandas DataFrame to CSV file with Python?

To write a pandas DataFrame to CSV file with Python, w ecan use the to_csv method.

For instance, we write

df.to_csv(file_name, sep='\t', encoding='utf-8')

to call to_csv to write the df data frame’s data to the file at path file_name.

We also set the sep argument to set the item separator.

And we set the encoding argument to set the encoding for the file.

Conclusion

To write a pandas DataFrame to CSV file with Python, w ecan use the to_csv method.

Categories
Python Answers

How to remove a key from a Python dictionary?

Sometimes, we want to remove a key from a Python dictionary.

In this article, we’ll look at how to remove a key from a Python dictionary.

How to remove a key from a Python dictionary?

To remove a key from a Python dictionary, we can use the dict’s pop method.

For instance, we write

my_dict.pop('key', None)

to call pop with the key of the dict to remove and the default value to return if the entry with 'key' isn’t found in the list.

This works no matter if an entry with key 'key' is in my_dict or not.

If the key exists for sure, we can use del

del my_dict['key']

to remove the entry with key 'key' from my_dict.

Conclusion

To remove a key from a Python dictionary, we can use the dict’s pop method.

Categories
Python Answers

How to set time limit on raw_input with Python?

Sometimes, we want to set time limit on raw_input with Python.

In this article, we’ll look at how to set time limit on raw_input with Python.

How to set time limit on raw_input with Python?

To set time limit on raw_input with Python, we can use the threading module.

For instance, we write

import thread
import threading

def raw_input_with_timeout(prompt, timeout=30.0):
    print(prompt, end=' ')    
    timer = threading.Timer(timeout, thread.interrupt_main)
    astring = None
    try:
        timer.start()
        astring = input(prompt)
    except KeyboardInterrupt:
        pass
    timer.cancel()
    return astring

to create the raw_input_with_timeout function.

In it, we call threading.Timer to create a timer.

And then we call timer.start to start the timer.

Then we call input to prompt for the input.

Next, we cal timer.cancel to cancel the timer once input is read.

And then we return the string once a value is entered or the timer timed out.

This works on Windows and Unix based OS’s.

Conclusion

To set time limit on raw_input with Python, we can use the threading module.

Categories
Python Answers

How to read multiple lines of raw input with Python?

Sometimes, we want to read multiple lines of raw input with Python.

In this article, we’ll look at how to read multiple lines of raw input with Python.

How to read multiple lines of raw input with Python?

To read multiple lines of raw input with Python, we call the iter function.

For instance, we write

sentinel = ''
for line in iter(input, sentinel):
    pass 

to call iter with input and the sentinel value to create an iterator that reads multiple lines of raw input until the sentinenl value is entered.

Then we loop through the entered values with a for loop and get the entered value from line.

Conclusion

To read multiple lines of raw input with Python, we call the iter function.

Categories
Python Answers

How to sort a Python list by two fields?

Sometimes, we want to sort a Python list by two fields.

In this article, we’ll look at how to sort a Python list by two fields.

How to sort a Python list by two fields?

To sort a Python list by two fields, we can use the sorted function.

For instance, we write

sorted_list = sorted(list, key=lambda x: (x[0], -x[1]))

to call sorted to return the list items sorted by calling it with the key argument set to a lamnda function that has a tuple of values to sort by.

We sort x[1] in descending order since we have a minus sign before the value.

Then we assign the returned sorted list to sorted_list.

Conclusion

To sort a Python list by two fields, we can use the sorted function.