Categories
Python Answers

How to select rows from a DataFrame based on column values with Python Pandas?

To select rows from a DataFrame based on column values with Python Pandas, we can use the loc property.

For instance, we write

df.loc[df['column_name'] == some_value]

to use the df.loc fictionary to get the column value with the df['column_name'] == some_value scalar.

We can combine conditions with & or |.

For instance, we write

df.loc[(df['column_name'] >= A) & (df['column_name'] <= B)]

to return the column values satifying df['column_name'] >= A and df['column_name'] <= B.

Categories
Python Answers

How to iterate over rows in a DataFrame in Python Pandas?

To iterate over rows in a DataFrame in Python Pandas, we can use a for loop.

For instance, we write

import pandas as pd

df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})
df = df.reset_index()  # make sure indexes pair with number of rows
for index, row in df.iterrows():
    print(row['c1'], row['c2'])

to loop through the df dataframe with iterator returned by the df.iterrows method.

We use the row object to get the rows and the keys to get the values.

Categories
Python Answers

How to set the background color for react-select drop downs?

Sometimes, we want to set the background color for react-select drop downs.

In this article, we’ll look at how to set the background color for react-select drop downs.

How to set the background color for react-select drop downs?

To set the background color for react-select drop downs, we can return an object with the color values set.

For instance, we write:

import React from "react";
import Select from "react-select";

const customStyles = {
  control: (base, state) => ({
    ...base,
    background: "#023950",
    borderRadius: state.isFocused ? "3px 3px 0 0" : 3,
    borderColor: state.isFocused ? "yellow" : "green",
    boxShadow: state.isFocused ? null : null,
    "&:hover": {
      borderColor: state.isFocused ? "red" : "blue"
    }
  }),
  menu: (base) => ({
    ...base,
    borderRadius: 0,
    marginTop: 0
  }),
  menuList: (base) => ({
    ...base,
    padding: 0
  })
};

const options = [
  { label: "Apple", value: "apple" },
  { label: "Orange", value: "orange" }
];

export default function App() {
  return (
    <form>
      <Select styles={customStyles} options={options} />
    </form>
  );
}

We set the styles prop to the customStyles object which has various styles.

The control method in the object returns an object with the style values.

The properties returned includes background, borderRadius, borderColor, boxShadow and other CSS style properties.

We can also style states like hover with "&:hover".

And we can get the state of the drop down from the state parameter.

Likewise, we have the menu and menuList methods to style the menu.

We set the options prop to an array of options.

Now we see we the drop down has a dark blue background and the drop down row that’s hovered over has a light blue background.

Conclusion

To set the background color for react-select drop downs, we can return an object with the color values set.

Categories
Python Answers

How to merge several Python dictionaries?

Sometimes, we want to merge several Python dictionaries.

In this article, we’ll look at how to merge several Python dictionaries.

How to merge several Python dictionaries?

To merge several Python dictionaries, we can use the ** operator to unpack dictionary entries into another dictionary.

For instance, we write:

a = {'a': 1, 'b': 2, 'c': 3}
b = {'d': 1, 'e': 2, 'f': 3}
c = {1: 1, 2: 2, 3: 3}
merge = {**a, **b, **c}
print(merge)

to merge the entries of a, b, and c into the merge dictionary by unpacking the entries from a, b, and c with the ** operator.

Therefore, merge is {'a': 1, 'b': 2, 'c': 3, 'd': 1, 'e': 2, 'f': 3, 1: 1, 2: 2, 3: 3}.

Conclusion

To merge several Python dictionaries, we can use the ** operator to unpack dictionary entries into another dictionary.

Categories
Python Answers

How to do locale date formatting in Python?

Sometimes, we want to do locale date formatting in Python.

In this article, we’ll look at how to do locale date formatting in Python.

How to do locale date formatting in Python?

To do locale date formatting in Python, we can use the locale and datetime modules

For instance, we write:

import locale
import datetime

locale.setlocale(locale.LC_TIME, '')
date_format = locale.nl_langinfo(locale.D_FMT)
d = datetime.date(2020, 4, 23)
print(d.strftime(date_format))

We call locale.setlocale to set the locale of the script.

Then we get the date format with:

date_format = locale.nl_langinfo(locale.D_FMT)

Finally, we create the date with datetime.date and format it into a date string with strftime with date_format as its argument.

Therefore, we see '04/23/2020' printed.

Conclusion

To do locale date formatting in Python, we can use the locale and datetime modules