create new dataframe with columns from another dataframe pandas

new = old[['A', 'C', 'D']].copy()

Here is what the above code is Doing:
1. We’re creating a new DataFrame called df_new.
2. We’re using the .copy() method to create a copy of the old DataFrame.
3. We’re using the .loc[] method to select the columns we want to keep.
4. We’re assigning the resulting DataFrame to df_new.

Note that we’re using the .copy() method to create a copy of the old DataFrame. This is important because it prevents the SettingWithCopyWarning from being raised.

If you don’t use the .copy() method, you’ll get the following warning:

SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
df_new[col] = df_new[col].str.lower()

This warning is telling you that you’re trying to set a value on a copy of a DataFrame. This can cause problems because the copy might not be updated.

To avoid this warning, you can either use the .copy() method or you can use the .loc[] method.

The .loc[] method is a powerful method that allows you to select rows and columns by label.

To select a single column, you can use the following syntax:

df.loc[:,’column_name’]

To select multiple columns, you can use the following syntax:

df.loc[:,[‘column_1′,’column_2’]]

You can also use the .loc[] method to select rows by label.

To select a single row, you can use the following syntax:

df.loc[row_label]

To select multiple rows, you can use the following syntax:

df.loc[[row_label_1,row_label_2]]

You can also use the .loc[] method to select both rows and columns by label.

To select a single row and column, you can use the following syntax:

df.loc[row_label, ‘column_name’]

To select multiple rows and columns, you can use the following syntax:

df.loc[[row_label_1, row_label_2], [‘column_1’, ‘column_2’]]

The .loc[] method is a powerful method that allows you to select rows and columns by label. However, it can be a little confusing to use at first.

In this tutorial, you’ve learned:

1. How to create a new DataFrame from an existing DataFrame.
2. How to select columns from a DataFrame.
3. How to select rows from a DataFrame.
4. How to select both rows and columns from a DataFrame.