Saturnix
Saturnix

Reputation: 10564

Replacing all values in a Pandas column, with no conditions

I have a Pandas dataframe with a column full of values I want to replace with another, non conditionally.

For the purpose of this question, let's assume I don't know how long this column is and I don't want to iterate over its values.

Using .replace() is not appropriate since I don't know which values are in that column: I want to replace all values, non conditionally.

Using df.loc[<row selection>, <column selection>] is not appropriate since there is no row selection logic: I want all the rows and simply writing True (as in data.loc[True, 'ColumnName'] = new_value) returns KeyError(True,). I tried data.loc[1, 'ColumnName'] = new_value and it works but it really looks like a shitty solution.

If I know len() of data['ColumnName'] I could create an array of that size, filled with as many time of my new_value and simply replace the column with that array. 10 lines of code to do something simpler than something that requires 1 line of code (doing so conditionally): this is also not ok.

How can I tell Pandas in 1 line: all the values in ColumnName are now new_value? I refuse to believe there's no way to tell Pandas not to bother me with conditions.

Upvotes: 9

Views: 13375

Answers (2)

yoonghm
yoonghm

Reputation: 4625

import pandas as pd

df = pd.DataFrame(
   {'num1'        : [3, 5, 9, 9, 14, 1],
    'num2'        : [3, 5, 9, 9, 14, 1]},
     index=[0, 1, 2, 3, 4, 5])
print(df)
print('\n')
df['num1'] = 100
print(df)
df['num1'] = 'Hi'
print('\n')
print(df)

The output is

  num1  num2
0     3     3
1     5     5
2     9     9
3     9     9
4    14    14
5     1     1


   num1  num2
0   100     3
1   100     5
2   100     9
3   100     9
4   100    14
5   100     1


  num1  num2
0   Hi     3
1   Hi     5
2   Hi     9
3   Hi     9
4   Hi    14
5   Hi     1

Upvotes: 1

Vishnu Kunchur
Vishnu Kunchur

Reputation: 1726

As I explained in the comment, you don't need to create an array. Let's say you have df:

  InvoiceNO Month  Year Size
0         1     1     2    7
1         2     1     2    8
2         3     2     2   11
3         4     3     2    9
4         5     7     2  8.5

..and you want to change all values in InvoiceNO to 1234:

df['InvoiceNO'] = 1234

Output:

   InvoiceNO Month  Year Size
0       1234     1     2    7
1       1234     1     2    8
2       1234     2     2   11
3       1234     3     2    9
4       1234     7     2  8.5

Upvotes: 15

Related Questions