How to write the value in a column for specific rows?
up vote
0
down vote
favorite
I have a excel file, i need to write the specific value in a set of specific row:
For Ex: i have 20 rows and 5 columns
i need to add new column, and write the new column value(as x in first 5 rows, next 5 values (y) in next 5 rows and so on).
may i know how to achieve it?
col1 col2 col3 col4
1 a1 b1 c1
2 a2 * *
3 a3 * *
4 a4 * *
5 a5 * *
6 a6 * *
7 a7 * *
8 a8 * *
9 a9 * *
10 a10 * *
11 a11 * *
12 a12 * *
13 a13 * *
14 a14 * *
15 a15 * *
16 a16 * c16
17 a17 * c17
18 a18 * c18
19 a19 * c19
I need output like this:
col1 col2 col3 col4 colnew
1 a1 b1 c1 aa
2 a2 * * aa
3 a3 * * aa
4 a4 * * aa
5 a5 * * aa
6 a6 * * bb
7 a7 * * bb
8 a8 * * bb
9 a9 * * bb
10 a10 * * bb
11 a11 * * cc
12 a12 * * cc
13 a13 * * cc
14 a14 * * cc
15 a15 * * cc
16 a16 * c16 dd
17 a17 * c17 dd
18 a18 * c18 dd
19 a19 * c19 dd
python excel pandas
add a comment |
up vote
0
down vote
favorite
I have a excel file, i need to write the specific value in a set of specific row:
For Ex: i have 20 rows and 5 columns
i need to add new column, and write the new column value(as x in first 5 rows, next 5 values (y) in next 5 rows and so on).
may i know how to achieve it?
col1 col2 col3 col4
1 a1 b1 c1
2 a2 * *
3 a3 * *
4 a4 * *
5 a5 * *
6 a6 * *
7 a7 * *
8 a8 * *
9 a9 * *
10 a10 * *
11 a11 * *
12 a12 * *
13 a13 * *
14 a14 * *
15 a15 * *
16 a16 * c16
17 a17 * c17
18 a18 * c18
19 a19 * c19
I need output like this:
col1 col2 col3 col4 colnew
1 a1 b1 c1 aa
2 a2 * * aa
3 a3 * * aa
4 a4 * * aa
5 a5 * * aa
6 a6 * * bb
7 a7 * * bb
8 a8 * * bb
9 a9 * * bb
10 a10 * * bb
11 a11 * * cc
12 a12 * * cc
13 a13 * * cc
14 a14 * * cc
15 a15 * * cc
16 a16 * c16 dd
17 a17 * c17 dd
18 a18 * c18 dd
19 a19 * c19 dd
python excel pandas
please read this before you post: stackoverflow.com/help/how-to-ask
– Zanshin
Nov 20 at 7:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a excel file, i need to write the specific value in a set of specific row:
For Ex: i have 20 rows and 5 columns
i need to add new column, and write the new column value(as x in first 5 rows, next 5 values (y) in next 5 rows and so on).
may i know how to achieve it?
col1 col2 col3 col4
1 a1 b1 c1
2 a2 * *
3 a3 * *
4 a4 * *
5 a5 * *
6 a6 * *
7 a7 * *
8 a8 * *
9 a9 * *
10 a10 * *
11 a11 * *
12 a12 * *
13 a13 * *
14 a14 * *
15 a15 * *
16 a16 * c16
17 a17 * c17
18 a18 * c18
19 a19 * c19
I need output like this:
col1 col2 col3 col4 colnew
1 a1 b1 c1 aa
2 a2 * * aa
3 a3 * * aa
4 a4 * * aa
5 a5 * * aa
6 a6 * * bb
7 a7 * * bb
8 a8 * * bb
9 a9 * * bb
10 a10 * * bb
11 a11 * * cc
12 a12 * * cc
13 a13 * * cc
14 a14 * * cc
15 a15 * * cc
16 a16 * c16 dd
17 a17 * c17 dd
18 a18 * c18 dd
19 a19 * c19 dd
python excel pandas
I have a excel file, i need to write the specific value in a set of specific row:
For Ex: i have 20 rows and 5 columns
i need to add new column, and write the new column value(as x in first 5 rows, next 5 values (y) in next 5 rows and so on).
may i know how to achieve it?
col1 col2 col3 col4
1 a1 b1 c1
2 a2 * *
3 a3 * *
4 a4 * *
5 a5 * *
6 a6 * *
7 a7 * *
8 a8 * *
9 a9 * *
10 a10 * *
11 a11 * *
12 a12 * *
13 a13 * *
14 a14 * *
15 a15 * *
16 a16 * c16
17 a17 * c17
18 a18 * c18
19 a19 * c19
I need output like this:
col1 col2 col3 col4 colnew
1 a1 b1 c1 aa
2 a2 * * aa
3 a3 * * aa
4 a4 * * aa
5 a5 * * aa
6 a6 * * bb
7 a7 * * bb
8 a8 * * bb
9 a9 * * bb
10 a10 * * bb
11 a11 * * cc
12 a12 * * cc
13 a13 * * cc
14 a14 * * cc
15 a15 * * cc
16 a16 * c16 dd
17 a17 * c17 dd
18 a18 * c18 dd
19 a19 * c19 dd
python excel pandas
python excel pandas
edited Nov 20 at 7:34
pygo
1,7291416
1,7291416
asked Nov 20 at 7:22
Sanakiyan Sundarrajan S
6
6
please read this before you post: stackoverflow.com/help/how-to-ask
– Zanshin
Nov 20 at 7:26
add a comment |
please read this before you post: stackoverflow.com/help/how-to-ask
– Zanshin
Nov 20 at 7:26
please read this before you post: stackoverflow.com/help/how-to-ask
– Zanshin
Nov 20 at 7:26
please read this before you post: stackoverflow.com/help/how-to-ask
– Zanshin
Nov 20 at 7:26
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Use floor division by 5
first and then map
by dictionary - if some value is missing in dict get NaN
s in output column:
vals = ['aa','bb','cc','dd','ee']
d = dict(enumerate(vals))
print (d)
{0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd', 4: 'ee'}
#default range index
df['new'] = (df.index // 5).map(d.get)
#general solution
#df['new'] = pd.Series(np.arange(len(df)) // 5, index=df.index).map(d)
print (df)
col1 col2 col3 col4 new
0 1 a1 b1 c1 aa
1 2 a2 * * aa
2 3 a3 * * aa
3 4 a4 * * aa
4 5 a5 * * aa
5 6 a6 * * bb
6 7 a7 * * bb
7 8 a8 * * bb
8 9 a9 * * bb
9 10 a10 * * bb
10 11 a11 * * cc
11 12 a12 * * cc
12 13 a13 * * cc
13 14 a14 * * cc
14 15 a15 * * cc
15 16 a16 * c16 dd
16 17 a17 * c17 dd
17 18 a18 * c18 dd
18 19 a19 * c19 dd
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Use floor division by 5
first and then map
by dictionary - if some value is missing in dict get NaN
s in output column:
vals = ['aa','bb','cc','dd','ee']
d = dict(enumerate(vals))
print (d)
{0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd', 4: 'ee'}
#default range index
df['new'] = (df.index // 5).map(d.get)
#general solution
#df['new'] = pd.Series(np.arange(len(df)) // 5, index=df.index).map(d)
print (df)
col1 col2 col3 col4 new
0 1 a1 b1 c1 aa
1 2 a2 * * aa
2 3 a3 * * aa
3 4 a4 * * aa
4 5 a5 * * aa
5 6 a6 * * bb
6 7 a7 * * bb
7 8 a8 * * bb
8 9 a9 * * bb
9 10 a10 * * bb
10 11 a11 * * cc
11 12 a12 * * cc
12 13 a13 * * cc
13 14 a14 * * cc
14 15 a15 * * cc
15 16 a16 * c16 dd
16 17 a17 * c17 dd
17 18 a18 * c18 dd
18 19 a19 * c19 dd
add a comment |
up vote
1
down vote
Use floor division by 5
first and then map
by dictionary - if some value is missing in dict get NaN
s in output column:
vals = ['aa','bb','cc','dd','ee']
d = dict(enumerate(vals))
print (d)
{0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd', 4: 'ee'}
#default range index
df['new'] = (df.index // 5).map(d.get)
#general solution
#df['new'] = pd.Series(np.arange(len(df)) // 5, index=df.index).map(d)
print (df)
col1 col2 col3 col4 new
0 1 a1 b1 c1 aa
1 2 a2 * * aa
2 3 a3 * * aa
3 4 a4 * * aa
4 5 a5 * * aa
5 6 a6 * * bb
6 7 a7 * * bb
7 8 a8 * * bb
8 9 a9 * * bb
9 10 a10 * * bb
10 11 a11 * * cc
11 12 a12 * * cc
12 13 a13 * * cc
13 14 a14 * * cc
14 15 a15 * * cc
15 16 a16 * c16 dd
16 17 a17 * c17 dd
17 18 a18 * c18 dd
18 19 a19 * c19 dd
add a comment |
up vote
1
down vote
up vote
1
down vote
Use floor division by 5
first and then map
by dictionary - if some value is missing in dict get NaN
s in output column:
vals = ['aa','bb','cc','dd','ee']
d = dict(enumerate(vals))
print (d)
{0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd', 4: 'ee'}
#default range index
df['new'] = (df.index // 5).map(d.get)
#general solution
#df['new'] = pd.Series(np.arange(len(df)) // 5, index=df.index).map(d)
print (df)
col1 col2 col3 col4 new
0 1 a1 b1 c1 aa
1 2 a2 * * aa
2 3 a3 * * aa
3 4 a4 * * aa
4 5 a5 * * aa
5 6 a6 * * bb
6 7 a7 * * bb
7 8 a8 * * bb
8 9 a9 * * bb
9 10 a10 * * bb
10 11 a11 * * cc
11 12 a12 * * cc
12 13 a13 * * cc
13 14 a14 * * cc
14 15 a15 * * cc
15 16 a16 * c16 dd
16 17 a17 * c17 dd
17 18 a18 * c18 dd
18 19 a19 * c19 dd
Use floor division by 5
first and then map
by dictionary - if some value is missing in dict get NaN
s in output column:
vals = ['aa','bb','cc','dd','ee']
d = dict(enumerate(vals))
print (d)
{0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd', 4: 'ee'}
#default range index
df['new'] = (df.index // 5).map(d.get)
#general solution
#df['new'] = pd.Series(np.arange(len(df)) // 5, index=df.index).map(d)
print (df)
col1 col2 col3 col4 new
0 1 a1 b1 c1 aa
1 2 a2 * * aa
2 3 a3 * * aa
3 4 a4 * * aa
4 5 a5 * * aa
5 6 a6 * * bb
6 7 a7 * * bb
7 8 a8 * * bb
8 9 a9 * * bb
9 10 a10 * * bb
10 11 a11 * * cc
11 12 a12 * * cc
12 13 a13 * * cc
13 14 a14 * * cc
14 15 a15 * * cc
15 16 a16 * c16 dd
16 17 a17 * c17 dd
17 18 a18 * c18 dd
18 19 a19 * c19 dd
edited Nov 20 at 7:34
answered Nov 20 at 7:28
jezrael
314k21250329
314k21250329
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53388079%2fhow-to-write-the-value-in-a-column-for-specific-rows%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
please read this before you post: stackoverflow.com/help/how-to-ask
– Zanshin
Nov 20 at 7:26