How to write the value in a column for specific rows?
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 |
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 |
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,8521516
1,8521516
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
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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
318k22257336
318k22257336
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