Python: Changing values in a DataFrame
up vote
0
down vote
favorite
I'm new to python and pandas and I need some ideas. Say I have the following DataFrame:
0 1 2 3 4 5
1 5 5 5 5 5
2 5 5 5 5 5
3 5 5 5 5 5
4 5 5 5 5 5
I want to iterate through each row and change the values of specific columns. Say I wanted to change all of the values in columns (2,3,4) to a 3.
This is what I've tried, am I going down the right path?
for row in df.iterrows():
for col in range(2, 4):
df.set_value('row', 'col', 3)
EDIT:
Thanks for the responses. The simple solutions are obvious, but what if I wanted to change the values to this... for example:
0 1 2 3 4 5
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20
python pandas dataframe
add a comment |
up vote
0
down vote
favorite
I'm new to python and pandas and I need some ideas. Say I have the following DataFrame:
0 1 2 3 4 5
1 5 5 5 5 5
2 5 5 5 5 5
3 5 5 5 5 5
4 5 5 5 5 5
I want to iterate through each row and change the values of specific columns. Say I wanted to change all of the values in columns (2,3,4) to a 3.
This is what I've tried, am I going down the right path?
for row in df.iterrows():
for col in range(2, 4):
df.set_value('row', 'col', 3)
EDIT:
Thanks for the responses. The simple solutions are obvious, but what if I wanted to change the values to this... for example:
0 1 2 3 4 5
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20
python pandas dataframe
Just assign a value to columndf[2]=3
,df[3]=3
,df[4]=3
– Sociopath
Nov 19 at 11:37
df[[2, 3, 4]] = 3
?
– jpp
Nov 19 at 11:38
There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?
– MEdwin
Nov 19 at 11:38
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm new to python and pandas and I need some ideas. Say I have the following DataFrame:
0 1 2 3 4 5
1 5 5 5 5 5
2 5 5 5 5 5
3 5 5 5 5 5
4 5 5 5 5 5
I want to iterate through each row and change the values of specific columns. Say I wanted to change all of the values in columns (2,3,4) to a 3.
This is what I've tried, am I going down the right path?
for row in df.iterrows():
for col in range(2, 4):
df.set_value('row', 'col', 3)
EDIT:
Thanks for the responses. The simple solutions are obvious, but what if I wanted to change the values to this... for example:
0 1 2 3 4 5
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20
python pandas dataframe
I'm new to python and pandas and I need some ideas. Say I have the following DataFrame:
0 1 2 3 4 5
1 5 5 5 5 5
2 5 5 5 5 5
3 5 5 5 5 5
4 5 5 5 5 5
I want to iterate through each row and change the values of specific columns. Say I wanted to change all of the values in columns (2,3,4) to a 3.
This is what I've tried, am I going down the right path?
for row in df.iterrows():
for col in range(2, 4):
df.set_value('row', 'col', 3)
EDIT:
Thanks for the responses. The simple solutions are obvious, but what if I wanted to change the values to this... for example:
0 1 2 3 4 5
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20
python pandas dataframe
python pandas dataframe
edited Nov 19 at 11:50
asked Nov 19 at 11:35
embedded.95
186
186
Just assign a value to columndf[2]=3
,df[3]=3
,df[4]=3
– Sociopath
Nov 19 at 11:37
df[[2, 3, 4]] = 3
?
– jpp
Nov 19 at 11:38
There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?
– MEdwin
Nov 19 at 11:38
add a comment |
Just assign a value to columndf[2]=3
,df[3]=3
,df[4]=3
– Sociopath
Nov 19 at 11:37
df[[2, 3, 4]] = 3
?
– jpp
Nov 19 at 11:38
There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?
– MEdwin
Nov 19 at 11:38
Just assign a value to column
df[2]=3
, df[3]=3
, df[4]=3
– Sociopath
Nov 19 at 11:37
Just assign a value to column
df[2]=3
, df[3]=3
, df[4]=3
– Sociopath
Nov 19 at 11:37
df[[2, 3, 4]] = 3
?– jpp
Nov 19 at 11:38
df[[2, 3, 4]] = 3
?– jpp
Nov 19 at 11:38
There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?
– MEdwin
Nov 19 at 11:38
There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?
– MEdwin
Nov 19 at 11:38
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
you can do this
df.iloc[:,1] = 3 #columns 2
df.iloc[:,2] = 3
df.iloc[:,3] = 3
add a comment |
up vote
2
down vote
If you are using a loop when working with dataframes, you are almost always not on the right track.
For this you can use a vectorized assignment:
df[[2, 3, 4]] = 3
Example:
df = pd.DataFrame({1: [1, 2], 2: [1, 2]})
print(df)
# 1 2
# 0 1 1
# 1 2 2
df[[1, 2]] = 3
print(df)
# 1 2
# 0 3 3
# 1 3 3
Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.
– embedded.95
Nov 19 at 11:45
@embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing ornp.where
.
– DeepSpace
Nov 19 at 11:49
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
you can do this
df.iloc[:,1] = 3 #columns 2
df.iloc[:,2] = 3
df.iloc[:,3] = 3
add a comment |
up vote
2
down vote
you can do this
df.iloc[:,1] = 3 #columns 2
df.iloc[:,2] = 3
df.iloc[:,3] = 3
add a comment |
up vote
2
down vote
up vote
2
down vote
you can do this
df.iloc[:,1] = 3 #columns 2
df.iloc[:,2] = 3
df.iloc[:,3] = 3
you can do this
df.iloc[:,1] = 3 #columns 2
df.iloc[:,2] = 3
df.iloc[:,3] = 3
edited Nov 19 at 11:39
Sociopath
3,09961431
3,09961431
answered Nov 19 at 11:38
runzhi xiao
813
813
add a comment |
add a comment |
up vote
2
down vote
If you are using a loop when working with dataframes, you are almost always not on the right track.
For this you can use a vectorized assignment:
df[[2, 3, 4]] = 3
Example:
df = pd.DataFrame({1: [1, 2], 2: [1, 2]})
print(df)
# 1 2
# 0 1 1
# 1 2 2
df[[1, 2]] = 3
print(df)
# 1 2
# 0 3 3
# 1 3 3
Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.
– embedded.95
Nov 19 at 11:45
@embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing ornp.where
.
– DeepSpace
Nov 19 at 11:49
add a comment |
up vote
2
down vote
If you are using a loop when working with dataframes, you are almost always not on the right track.
For this you can use a vectorized assignment:
df[[2, 3, 4]] = 3
Example:
df = pd.DataFrame({1: [1, 2], 2: [1, 2]})
print(df)
# 1 2
# 0 1 1
# 1 2 2
df[[1, 2]] = 3
print(df)
# 1 2
# 0 3 3
# 1 3 3
Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.
– embedded.95
Nov 19 at 11:45
@embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing ornp.where
.
– DeepSpace
Nov 19 at 11:49
add a comment |
up vote
2
down vote
up vote
2
down vote
If you are using a loop when working with dataframes, you are almost always not on the right track.
For this you can use a vectorized assignment:
df[[2, 3, 4]] = 3
Example:
df = pd.DataFrame({1: [1, 2], 2: [1, 2]})
print(df)
# 1 2
# 0 1 1
# 1 2 2
df[[1, 2]] = 3
print(df)
# 1 2
# 0 3 3
# 1 3 3
If you are using a loop when working with dataframes, you are almost always not on the right track.
For this you can use a vectorized assignment:
df[[2, 3, 4]] = 3
Example:
df = pd.DataFrame({1: [1, 2], 2: [1, 2]})
print(df)
# 1 2
# 0 1 1
# 1 2 2
df[[1, 2]] = 3
print(df)
# 1 2
# 0 3 3
# 1 3 3
edited 2 days ago
answered Nov 19 at 11:38
DeepSpace
34.1k43765
34.1k43765
Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.
– embedded.95
Nov 19 at 11:45
@embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing ornp.where
.
– DeepSpace
Nov 19 at 11:49
add a comment |
Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.
– embedded.95
Nov 19 at 11:45
@embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing ornp.where
.
– DeepSpace
Nov 19 at 11:49
Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.
– embedded.95
Nov 19 at 11:45
Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.
– embedded.95
Nov 19 at 11:45
@embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or
np.where
.– DeepSpace
Nov 19 at 11:49
@embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or
np.where
.– DeepSpace
Nov 19 at 11:49
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373798%2fpython-changing-values-in-a-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just assign a value to column
df[2]=3
,df[3]=3
,df[4]=3
– Sociopath
Nov 19 at 11:37
df[[2, 3, 4]] = 3
?– jpp
Nov 19 at 11:38
There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?
– MEdwin
Nov 19 at 11:38