Copy All Values in a Column to a New Column in a Pandas Dataframe

Copy all values in a column to a new column in a pandas dataframe

You can simply assign the B to the new column , Like -

df['D'] = df['B']

Example/Demo -

In [1]: import pandas as pd

In [2]: df = pd.DataFrame([['a.1','b.1','c.1'],['a.2','b.2','c.2'],['a.3','b.3','c.3']],columns=['A','B','C'])

In [3]: df
Out[3]:
A B C
0 a.1 b.1 c.1
1 a.2 b.2 c.2
2 a.3 b.3 c.3

In [4]: df['D'] = df['B'] #<---What you want.

In [5]: df
Out[5]:
A B C D
0 a.1 b.1 c.1 b.1
1 a.2 b.2 c.2 b.2
2 a.3 b.3 c.3 b.3

In [6]: df.loc[0,'D'] = 'd.1'

In [7]: df
Out[7]:
A B C D
0 a.1 b.1 c.1 d.1
1 a.2 b.2 c.2 b.2
2 a.3 b.3 c.3 b.3

How to copy the value of a column into a new column

You can always do

df['new_data'] = df['DATA_2'].shift(-2).fillna(0)

How do I copy contents from one column to another while using .apply() in pandas?

We can use Series.mask or Series.where, series.mask set to NaN the rows where 'invoice_currency' is USD, but with the other parameter we tell it that these values ​​have to be filled with df_data['total_open_amount'] series multiplied by 0.7.

using serie.where the rows that do not meet the condition are set to NaN, so first we multiply the series by 0.7 and leave only the rows where the condition is met, that is, the rows with USD currency and we use other parameter to leave the rest of rows with initial value

Note that series.mask and series.where are the opposite of each other.

df_data['converted_usd'] = df_data['total_open_amount']\
.mask(df_data['invoice_currency'] == 'CAD',
other=df_data['total_open_amount'].mul(0.7))

Or:

df_data['converted_usd'] = df_data['total_open_amount'].mul(0.7)\
.where(df_data['invoice_currency'] == 'CAD',
df_data['total_open_amount'])

numpy version

df_data['converted_usd'] = \
np.where(df_data['invoice_currency'] == 'CAD',
df_data['total_open_amount'].mul(0.7),
df_data['total_open_amount'])

How to copy value from one column to another based on conditions in pythons dataframe?

Write the conditions on columns as you have described in the question and assign the output to the value.
One option is to use numpy.where for such cases. Note that there are many other ways to achieve the same goal.

import numpy as np
import pandas as pd

# You don't need this part of the code, I used it to recreate the
# dataframe, as you have shared a link to a screenshot..
df = pd.DataFrame({
'operator': np.array([[i] * 3 for i in ['First', 'Second', 'Third']]).flatten(),
'solution': ['row'] * 9,
'rule': ['allow', 'block', 'teleport'] * 3,
'value': [1, 0, '', 0, 1, 'loc', 1, 1, ''],
'teleport_value': ['', '', 'value'] + [''] * 5 + ['value']
})


# Solution: I have put the comments with your own words from question,
# so that you know what does each line..
df['value'] = np.where(
(
(df['rule'] == 'teleport') & # row with column rule is 'teleport' AND
(df['value'] == '') # column value is ''
),
df['teleport_value'], # copy teleport_value to value column
df['value'] # in other cases the field of column value will be without changes
)

Copy values from one column to another using Pandas

You can simply use the fillna() function available in pandas to solve this very efficiently.

Below code explains how to it in Python.

df = pd.DataFrame()

df['X'] = [0, 3, 1, 1, 2, 2, 3, 3, 1, 2]
df['Y'] = [111.0, np.nan, np.nan, 112, 113, np.nan, 114, 115, np.nan, 116]

df['Y'] = df['Y'].fillna(df['X'])

print(df)

Pandas - copy dataframe base on values in a new column

In your case try with merge with cross

m = pd.DataFrame({'C':['Orange','Apple']})
df.merge(m,how='cross')
Out[151]:
A B C
0 1 2 Orange
1 1 2 Apple
2 3 4 Orange
3 3 4 Apple

Copy a column to multiple columns of a DataFrame with Pandas

If you're interested in replacing values of columns that contain all nulls, you can take a shortcut and simply overwrite all values below row 2 after identifying those values are entirely null.

# Identify columns that contain null values from row 2 onwards
all_null_cols = df.loc[2:].isnull().all()

# overwrite row 2 onwards in only our null columns with values from "EZ19"
df.loc[2:, all_nulls] = df.loc[2:, ["EZ19"]].values

print(df)
GeoCode ESP FIN USA EZ19 PRT
1 Geography Spain Finland USA EZ Portugal
2 31-Mar-15 0.89 0.89 0.26 0.89 0.89
3 30-Jun-15 0.90 0.90 NaN 0.90 0.90
4 30-Sep-15 0.90 0.90 0.31 0.90 0.90
5 31-Dec-15 0.91 0.91 0.41 0.91 0.91

Python - Pandas - Copy column names to new dataframe without bringing data

You could do it like this:

new_df = df.copy()
new_df[['5:10', '6:10', '7:10']] = ''

or more concise:

new_df = df.copy()
new_df[new_df.columns[1:]] = ''

But why not just create a new dataframe with new_df = df.copy() and then perform your computations without blanking the dataframe? I don't think you need to do that, and it just adds time to the process.

How to copy specific value from one column to another in pandas

Use pandas.Series.str.isnumeric with np.where

df['ID'] = np.where(df.Name.str.isnumeric(), df.Name, df.ID)

Output

      ID    Name Age
0 54789 John 24
1 62549 62549 28
2 3547 Maggie 31
3 1345 1345 21
4 nan Luke 22

Pandas: copy value from row above of another column when condition match

You can use boolean masks eq/ne, and shift to get the previous values, then select with where:

# is the previous input1 equal to 5?
m1 = df['input1'].shift().eq(5)
# is event not 0?
m2 = df['event'].ne(0)

# get the previous label if both conditions are true, else 0
df['marker'] = df['label'].shift(fill_value=0).where(m1&m2, 0)
# OR
# df['marker'] = df['label'].shift().where(m1&m2, 0).convert_dtypes()

output:

   input1  input2  input3  event  label  marker
0 0 0 0 0 0 0
1 5 5 0 0 2 0
2 5 5 0 0 2 0
3 0 0 0 24 0 2
4 0 0 0 0 0 0
5 5 0 5 0 3 0
6 5 0 5 0 3 0
7 5 0 5 0 3 0
8 0 0 0 25 0 3
9 0 0 0 0 0 0


Related Topics



Leave a reply



Submit