how to overwrite values of first row of dataframe

laszlopanaflex

Given a panda.Dataframe such as:

df = pd.DataFrame(np.random.randn(10,5), columns = ['a','b','c','d','e'])

I would like to know the best way to replace all values in the first row with a 0 (or some other specific value) and work with the new dataframe. I would like to do this in a general way, where there may be more or less columns than in this example.

Despite the simplicity of the question, I was not able to come across a solution. Most examples posted by others had to do with fillna() and related methods

Stephen Rauch

You can use iloc to do that pretty cleanly like:

Code:

df.iloc[0] = 0

Test Code:

df = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e'])
print(df)
df.iloc[0] = 0
print(df)

Results:

          a         b         c         d         e
0  0.715524 -0.914676  0.241008 -1.353033  0.170578
1 -0.300348  1.118491 -0.520407  0.185877 -0.950839
2  1.942239  0.980477  0.110457 -0.558483  0.903775
3  0.400923  1.347769 -0.120445  0.036253  0.683571
4 -0.761881 -0.642469  2.030019  2.274070 -0.067672
5  0.566003  0.263949 -0.567247  0.689599  0.870442
6  1.904812 -0.689312  1.400950  1.942681 -1.268679
7 -0.253381  0.464208  1.362960  0.129433  0.527576
8 -1.404035  0.174586  1.006268  0.007333  1.172559
9  0.330404  0.735610  1.277451 -0.104888  0.528356

          a         b         c         d         e
0  0.000000  0.000000  0.000000  0.000000  0.000000
1 -0.300348  1.118491 -0.520407  0.185877 -0.950839
2  1.942239  0.980477  0.110457 -0.558483  0.903775
3  0.400923  1.347769 -0.120445  0.036253  0.683571
4 -0.761881 -0.642469  2.030019  2.274070 -0.067672
5  0.566003  0.263949 -0.567247  0.689599  0.870442
6  1.904812 -0.689312  1.400950  1.942681 -1.268679
7 -0.253381  0.464208  1.362960  0.129433  0.527576
8 -1.404035  0.174586  1.006268  0.007333  1.172559
9  0.330404  0.735610  1.277451 -0.104888  0.528356

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

pandas dataframe groupby and fill with first row values

分類Dev

Apply on Dataframe passes first row values to all rows

分類Dev

How to count nonblank values in each dataframe row

分類Dev

Merge with overwrite values from left dataframe in pandas

分類Dev

How to overwrite the values of the first column in every rows in a HTML tbody table with JavaScript and with jQuery?

分類Dev

How to get the first row values in sql server query

分類Dev

How to take the first two values but not equal to a specific value for each row

分類Dev

how to "overwrite-merge" a pandas dataframe

分類Dev

How to reduce some values from a list within each row in a dataframe

分類Dev

How I obtain values from another row to place in the current row in a pandas dataframe?

分類Dev

Pandas how to add additional values between two row-values in a dataframe

分類Dev

How can I get the first 4 row values into one row in seperate columns for each visit_id record?

分類Dev

Categories columns in second row to the first row in DataFrame Pandas?

分類Dev

How to get the first row that appears first in the data?

分類Dev

Spark Dataframe performance for overwrite

分類Dev

R - Identical values in columns of dataframe in one row

分類Dev

Count of values in a row in spark dataframe using scala

分類Dev

conditional row-wide replacement of values in dataframe

分類Dev

Fill in missing row values in pandas dataframe

分類Dev

Add column to dataframe depending on specific row values

分類Dev

How do I go about selecting column data in a dataframe for specific row values in python?

分類Dev

How to subset a pandas dataframe based on iterating through all row values of another df?

分類Dev

Replacing values in dataframe by values from other rows by "target row"

分類Dev

How to plot values by row in pheatmap?

分類Dev

Pandas: Group by the first two characters of the row index of a dataframe

分類Dev

Deleting first few rows and change the header names to row values

分類Dev

Matrix conversion involving first and second largest values row-wise

分類Dev

How can I make the values of this dataframe add progressively. Ex. the second is equal to the first + second and so on, as continuous time

分類Dev

How to binarize the values in a pandas DataFrame?

Related 関連記事

  1. 1

    pandas dataframe groupby and fill with first row values

  2. 2

    Apply on Dataframe passes first row values to all rows

  3. 3

    How to count nonblank values in each dataframe row

  4. 4

    Merge with overwrite values from left dataframe in pandas

  5. 5

    How to overwrite the values of the first column in every rows in a HTML tbody table with JavaScript and with jQuery?

  6. 6

    How to get the first row values in sql server query

  7. 7

    How to take the first two values but not equal to a specific value for each row

  8. 8

    how to "overwrite-merge" a pandas dataframe

  9. 9

    How to reduce some values from a list within each row in a dataframe

  10. 10

    How I obtain values from another row to place in the current row in a pandas dataframe?

  11. 11

    Pandas how to add additional values between two row-values in a dataframe

  12. 12

    How can I get the first 4 row values into one row in seperate columns for each visit_id record?

  13. 13

    Categories columns in second row to the first row in DataFrame Pandas?

  14. 14

    How to get the first row that appears first in the data?

  15. 15

    Spark Dataframe performance for overwrite

  16. 16

    R - Identical values in columns of dataframe in one row

  17. 17

    Count of values in a row in spark dataframe using scala

  18. 18

    conditional row-wide replacement of values in dataframe

  19. 19

    Fill in missing row values in pandas dataframe

  20. 20

    Add column to dataframe depending on specific row values

  21. 21

    How do I go about selecting column data in a dataframe for specific row values in python?

  22. 22

    How to subset a pandas dataframe based on iterating through all row values of another df?

  23. 23

    Replacing values in dataframe by values from other rows by "target row"

  24. 24

    How to plot values by row in pheatmap?

  25. 25

    Pandas: Group by the first two characters of the row index of a dataframe

  26. 26

    Deleting first few rows and change the header names to row values

  27. 27

    Matrix conversion involving first and second largest values row-wise

  28. 28

    How can I make the values of this dataframe add progressively. Ex. the second is equal to the first + second and so on, as continuous time

  29. 29

    How to binarize the values in a pandas DataFrame?

ホットタグ

アーカイブ