Filter a data-frame and add a new column according to the given conditionAdd new keys to a dictionary?Adding new column to existing DataFrame in Python pandas“Large data” work flows using pandasAdd numpy array as column to Pandas data frameConvert Python dict into a dataframeAdd new column in Pandas DataFrame PythonPython: function default input that stands for everythingpython subset data frame by column valueMatch columns and append to data frame, Python 3.6How to pivot a dataframe
What is this minifig/minidoll (?)
Should generated documentation be stored in a Git repository?
Why doesn't Iron Man's action affect this person in Endgame?
Why is it harder to turn a motor/generator with shorted terminals?
Is this a group? If so, what group is it?
The meaning of the Middle English word “king”
Are there any sonatas with only two sections?
Is 95% of what you read in the financial press “either wrong or irrelevant?”
Given 0s on Assignments with suspected and dismissed cheating?
What information exactly does an instruction cache store?
Do not cross the line!
Is it wrong to omit object pronouns in these sentences?
Help understanding this line - usage of くれる
Wireless headphones interfere with Wi-Fi signal on laptop
How to continually let my readers know what time it is in my story, in an organic way?
Unexpected Netflix account registered to my Gmail address - any way it could be a hack attempt?
Formal Definition of Dot Product
"The van's really booking"
How do I adjust encounters to challenge my lycanthrope players without negating their cool new abilities?
How to disable Two-factor authentication for Apple ID?
Can my Serbian girlfriend apply for a UK Standard Visitor visa and stay for the whole 6 months?
Can only the master initiate communication in SPI whereas in I2C the slave can also initiate the communication?
Why didn't the Avengers use this object earlier?
Is this possible when it comes to the relations of P, NP, NP-Hard and NP-Complete?
Filter a data-frame and add a new column according to the given condition
Add new keys to a dictionary?Adding new column to existing DataFrame in Python pandas“Large data” work flows using pandasAdd numpy array as column to Pandas data frameConvert Python dict into a dataframeAdd new column in Pandas DataFrame PythonPython: function default input that stands for everythingpython subset data frame by column valueMatch columns and append to data frame, Python 3.6How to pivot a dataframe
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a data frame like this
ID col1 col2
1 Abc street 2017-07-27
1 None 2017-08-17
1 Def street 2018-07-15
1 None 2018-08-13
2 fbg street 2018-01-07
2 None 2018-08-12
2 trf street 2019-01-15
I want to filter all the 'None' from col1 and add the corresponding col2 value into a new column col3. My output look like this
ID col1 col2 col3
1 Abc street 2017-07-27 2017-08-17
1 Def street 2018-07-15 2018-08-13
2 fbg street 2018-01-07 2018-08-12
2 trf street 2019-01-15
Can anyone help me to achieve this.
python python-3.x pandas numpy pandas-groupby
add a comment |
I have a data frame like this
ID col1 col2
1 Abc street 2017-07-27
1 None 2017-08-17
1 Def street 2018-07-15
1 None 2018-08-13
2 fbg street 2018-01-07
2 None 2018-08-12
2 trf street 2019-01-15
I want to filter all the 'None' from col1 and add the corresponding col2 value into a new column col3. My output look like this
ID col1 col2 col3
1 Abc street 2017-07-27 2017-08-17
1 Def street 2018-07-15 2018-08-13
2 fbg street 2018-01-07 2018-08-12
2 trf street 2019-01-15
Can anyone help me to achieve this.
python python-3.x pandas numpy pandas-groupby
Is itNone
or'None'
?
– user3483203
6 hours ago
oh!!. it's actually 'None'
– No_body
6 hours ago
add a comment |
I have a data frame like this
ID col1 col2
1 Abc street 2017-07-27
1 None 2017-08-17
1 Def street 2018-07-15
1 None 2018-08-13
2 fbg street 2018-01-07
2 None 2018-08-12
2 trf street 2019-01-15
I want to filter all the 'None' from col1 and add the corresponding col2 value into a new column col3. My output look like this
ID col1 col2 col3
1 Abc street 2017-07-27 2017-08-17
1 Def street 2018-07-15 2018-08-13
2 fbg street 2018-01-07 2018-08-12
2 trf street 2019-01-15
Can anyone help me to achieve this.
python python-3.x pandas numpy pandas-groupby
I have a data frame like this
ID col1 col2
1 Abc street 2017-07-27
1 None 2017-08-17
1 Def street 2018-07-15
1 None 2018-08-13
2 fbg street 2018-01-07
2 None 2018-08-12
2 trf street 2019-01-15
I want to filter all the 'None' from col1 and add the corresponding col2 value into a new column col3. My output look like this
ID col1 col2 col3
1 Abc street 2017-07-27 2017-08-17
1 Def street 2018-07-15 2018-08-13
2 fbg street 2018-01-07 2018-08-12
2 trf street 2019-01-15
Can anyone help me to achieve this.
python python-3.x pandas numpy pandas-groupby
python python-3.x pandas numpy pandas-groupby
asked 6 hours ago
No_bodyNo_body
359214
359214
Is itNone
or'None'
?
– user3483203
6 hours ago
oh!!. it's actually 'None'
– No_body
6 hours ago
add a comment |
Is itNone
or'None'
?
– user3483203
6 hours ago
oh!!. it's actually 'None'
– No_body
6 hours ago
Is it
None
or 'None'
?– user3483203
6 hours ago
Is it
None
or 'None'
?– user3483203
6 hours ago
oh!!. it's actually 'None'
– No_body
6 hours ago
oh!!. it's actually 'None'
– No_body
6 hours ago
add a comment |
5 Answers
5
active
oldest
votes
Today's edition of Over Engineered with Numpy
Though admittedly very little obvious Numpy
i, rows = pd.factorize([*zip(df.ID, df.col1.replace('None'))])
k, cols = pd.factorize(df.groupby(i).cumcount())
dleft = pd.DataFrame(dict(zip(['ID', 'col1'], zip(*rows))))
drigt = pd.DataFrame(index=dleft.index, columns=np.arange(len(cols)) + 2).add_prefix('col')
drigt.values[i, k] = df.col2.values
dleft.join(drigt)
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
add a comment |
Using ffill
+ pivot_table
. This assumes that None
follows the proper value, which it appears to from your data.
u = df.assign(col1=df.col1.replace('None'))
g = ['ID', 'col1']
idx = u.groupby(g).cumcount()
(u.assign(idx=idx)
.pivot_table(index=g, columns='idx', values='col2', aggfunc='first')
.reset_index())
idx ID col1 0 1
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
add a comment |
I am using cumcount
with merge
df1=df.loc[df.col1.ne('None'),:].copy()
df2=df.loc[df.col1.eq('None'),:].copy()
df1['Key']=df1.groupby('ID').cumcount()
df2['Key']=df2.groupby('ID').cumcount()
df1.merge(df2.drop('col1',1),on=['ID','Key'],how='left')
Out[816]:
ID col1 col2_x Key col2_y
0 1 Abcstreet 2017-07-27 0 2017-08-17
1 1 Defstreet 2018-07-15 1 2018-08-13
2 2 fbgstreet 2018-01-07 0 2018-08-12
3 2 trfstreet 2019-01-15 1 NaN
It's working fine but failing for this condition. When i have ab street 2018-01-07 , bc street 2018-02-08 , None 2018-08-12 , rf street 2019-01-15 . The output coming as ab street 2018-01-07 , 2018-08-12 , bc street 2018-02-08 , rf street 2019-01-15 ... Instead of ab street 2018-01-07 , bc street 2018-02-08 , 2018-08-12 , rf street 2019-01-15 .
– No_body
5 hours ago
add a comment |
Try:
filters = df['col1'].isna()
s = df.loc[filters, 'col2'].copy()
df = df[~filters]
df['col3'] = s.values
Edit: as you mentioned, the filter you want is 'None'
, not None
, then:
filters = df['col1'].eq('None')
Maybe you need to check some edge situation , I thought the ID is the key to assign , if you only assign it by value , ID may mismatch
– WeNYoBen
6 hours ago
Agree with @WeNYoBen, its dangerous just topaste
the values as a column
– Erfan
6 hours ago
That's true. But theID
column given is not unique-value, so assign on that would fail (I think). Or I need to do a groupby.
– Quang Hoang
6 hours ago
add a comment |
Yet another attempt:
f=df['col1']=='None'
c3=df.loc[f].col2.reset_index(drop=True)
df=df[~f]
df2=pd.concat([df.reset_index(drop=True),c3], axis=1, ignore_index=True)
df2.columns=['ID', 'col1', 'col2', 'col3']
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
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%2f56117560%2ffilter-a-data-frame-and-add-a-new-column-according-to-the-given-condition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Today's edition of Over Engineered with Numpy
Though admittedly very little obvious Numpy
i, rows = pd.factorize([*zip(df.ID, df.col1.replace('None'))])
k, cols = pd.factorize(df.groupby(i).cumcount())
dleft = pd.DataFrame(dict(zip(['ID', 'col1'], zip(*rows))))
drigt = pd.DataFrame(index=dleft.index, columns=np.arange(len(cols)) + 2).add_prefix('col')
drigt.values[i, k] = df.col2.values
dleft.join(drigt)
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
add a comment |
Today's edition of Over Engineered with Numpy
Though admittedly very little obvious Numpy
i, rows = pd.factorize([*zip(df.ID, df.col1.replace('None'))])
k, cols = pd.factorize(df.groupby(i).cumcount())
dleft = pd.DataFrame(dict(zip(['ID', 'col1'], zip(*rows))))
drigt = pd.DataFrame(index=dleft.index, columns=np.arange(len(cols)) + 2).add_prefix('col')
drigt.values[i, k] = df.col2.values
dleft.join(drigt)
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
add a comment |
Today's edition of Over Engineered with Numpy
Though admittedly very little obvious Numpy
i, rows = pd.factorize([*zip(df.ID, df.col1.replace('None'))])
k, cols = pd.factorize(df.groupby(i).cumcount())
dleft = pd.DataFrame(dict(zip(['ID', 'col1'], zip(*rows))))
drigt = pd.DataFrame(index=dleft.index, columns=np.arange(len(cols)) + 2).add_prefix('col')
drigt.values[i, k] = df.col2.values
dleft.join(drigt)
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
Today's edition of Over Engineered with Numpy
Though admittedly very little obvious Numpy
i, rows = pd.factorize([*zip(df.ID, df.col1.replace('None'))])
k, cols = pd.factorize(df.groupby(i).cumcount())
dleft = pd.DataFrame(dict(zip(['ID', 'col1'], zip(*rows))))
drigt = pd.DataFrame(index=dleft.index, columns=np.arange(len(cols)) + 2).add_prefix('col')
drigt.values[i, k] = df.col2.values
dleft.join(drigt)
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
answered 6 hours ago
piRSquaredpiRSquared
163k25166313
163k25166313
add a comment |
add a comment |
Using ffill
+ pivot_table
. This assumes that None
follows the proper value, which it appears to from your data.
u = df.assign(col1=df.col1.replace('None'))
g = ['ID', 'col1']
idx = u.groupby(g).cumcount()
(u.assign(idx=idx)
.pivot_table(index=g, columns='idx', values='col2', aggfunc='first')
.reset_index())
idx ID col1 0 1
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
add a comment |
Using ffill
+ pivot_table
. This assumes that None
follows the proper value, which it appears to from your data.
u = df.assign(col1=df.col1.replace('None'))
g = ['ID', 'col1']
idx = u.groupby(g).cumcount()
(u.assign(idx=idx)
.pivot_table(index=g, columns='idx', values='col2', aggfunc='first')
.reset_index())
idx ID col1 0 1
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
add a comment |
Using ffill
+ pivot_table
. This assumes that None
follows the proper value, which it appears to from your data.
u = df.assign(col1=df.col1.replace('None'))
g = ['ID', 'col1']
idx = u.groupby(g).cumcount()
(u.assign(idx=idx)
.pivot_table(index=g, columns='idx', values='col2', aggfunc='first')
.reset_index())
idx ID col1 0 1
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
Using ffill
+ pivot_table
. This assumes that None
follows the proper value, which it appears to from your data.
u = df.assign(col1=df.col1.replace('None'))
g = ['ID', 'col1']
idx = u.groupby(g).cumcount()
(u.assign(idx=idx)
.pivot_table(index=g, columns='idx', values='col2', aggfunc='first')
.reset_index())
idx ID col1 0 1
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
edited 6 hours ago
answered 6 hours ago
user3483203user3483203
33.3k83157
33.3k83157
add a comment |
add a comment |
I am using cumcount
with merge
df1=df.loc[df.col1.ne('None'),:].copy()
df2=df.loc[df.col1.eq('None'),:].copy()
df1['Key']=df1.groupby('ID').cumcount()
df2['Key']=df2.groupby('ID').cumcount()
df1.merge(df2.drop('col1',1),on=['ID','Key'],how='left')
Out[816]:
ID col1 col2_x Key col2_y
0 1 Abcstreet 2017-07-27 0 2017-08-17
1 1 Defstreet 2018-07-15 1 2018-08-13
2 2 fbgstreet 2018-01-07 0 2018-08-12
3 2 trfstreet 2019-01-15 1 NaN
It's working fine but failing for this condition. When i have ab street 2018-01-07 , bc street 2018-02-08 , None 2018-08-12 , rf street 2019-01-15 . The output coming as ab street 2018-01-07 , 2018-08-12 , bc street 2018-02-08 , rf street 2019-01-15 ... Instead of ab street 2018-01-07 , bc street 2018-02-08 , 2018-08-12 , rf street 2019-01-15 .
– No_body
5 hours ago
add a comment |
I am using cumcount
with merge
df1=df.loc[df.col1.ne('None'),:].copy()
df2=df.loc[df.col1.eq('None'),:].copy()
df1['Key']=df1.groupby('ID').cumcount()
df2['Key']=df2.groupby('ID').cumcount()
df1.merge(df2.drop('col1',1),on=['ID','Key'],how='left')
Out[816]:
ID col1 col2_x Key col2_y
0 1 Abcstreet 2017-07-27 0 2017-08-17
1 1 Defstreet 2018-07-15 1 2018-08-13
2 2 fbgstreet 2018-01-07 0 2018-08-12
3 2 trfstreet 2019-01-15 1 NaN
It's working fine but failing for this condition. When i have ab street 2018-01-07 , bc street 2018-02-08 , None 2018-08-12 , rf street 2019-01-15 . The output coming as ab street 2018-01-07 , 2018-08-12 , bc street 2018-02-08 , rf street 2019-01-15 ... Instead of ab street 2018-01-07 , bc street 2018-02-08 , 2018-08-12 , rf street 2019-01-15 .
– No_body
5 hours ago
add a comment |
I am using cumcount
with merge
df1=df.loc[df.col1.ne('None'),:].copy()
df2=df.loc[df.col1.eq('None'),:].copy()
df1['Key']=df1.groupby('ID').cumcount()
df2['Key']=df2.groupby('ID').cumcount()
df1.merge(df2.drop('col1',1),on=['ID','Key'],how='left')
Out[816]:
ID col1 col2_x Key col2_y
0 1 Abcstreet 2017-07-27 0 2017-08-17
1 1 Defstreet 2018-07-15 1 2018-08-13
2 2 fbgstreet 2018-01-07 0 2018-08-12
3 2 trfstreet 2019-01-15 1 NaN
I am using cumcount
with merge
df1=df.loc[df.col1.ne('None'),:].copy()
df2=df.loc[df.col1.eq('None'),:].copy()
df1['Key']=df1.groupby('ID').cumcount()
df2['Key']=df2.groupby('ID').cumcount()
df1.merge(df2.drop('col1',1),on=['ID','Key'],how='left')
Out[816]:
ID col1 col2_x Key col2_y
0 1 Abcstreet 2017-07-27 0 2017-08-17
1 1 Defstreet 2018-07-15 1 2018-08-13
2 2 fbgstreet 2018-01-07 0 2018-08-12
3 2 trfstreet 2019-01-15 1 NaN
answered 6 hours ago
WeNYoBenWeNYoBen
133k84373
133k84373
It's working fine but failing for this condition. When i have ab street 2018-01-07 , bc street 2018-02-08 , None 2018-08-12 , rf street 2019-01-15 . The output coming as ab street 2018-01-07 , 2018-08-12 , bc street 2018-02-08 , rf street 2019-01-15 ... Instead of ab street 2018-01-07 , bc street 2018-02-08 , 2018-08-12 , rf street 2019-01-15 .
– No_body
5 hours ago
add a comment |
It's working fine but failing for this condition. When i have ab street 2018-01-07 , bc street 2018-02-08 , None 2018-08-12 , rf street 2019-01-15 . The output coming as ab street 2018-01-07 , 2018-08-12 , bc street 2018-02-08 , rf street 2019-01-15 ... Instead of ab street 2018-01-07 , bc street 2018-02-08 , 2018-08-12 , rf street 2019-01-15 .
– No_body
5 hours ago
It's working fine but failing for this condition. When i have ab street 2018-01-07 , bc street 2018-02-08 , None 2018-08-12 , rf street 2019-01-15 . The output coming as ab street 2018-01-07 , 2018-08-12 , bc street 2018-02-08 , rf street 2019-01-15 ... Instead of ab street 2018-01-07 , bc street 2018-02-08 , 2018-08-12 , rf street 2019-01-15 .
– No_body
5 hours ago
It's working fine but failing for this condition. When i have ab street 2018-01-07 , bc street 2018-02-08 , None 2018-08-12 , rf street 2019-01-15 . The output coming as ab street 2018-01-07 , 2018-08-12 , bc street 2018-02-08 , rf street 2019-01-15 ... Instead of ab street 2018-01-07 , bc street 2018-02-08 , 2018-08-12 , rf street 2019-01-15 .
– No_body
5 hours ago
add a comment |
Try:
filters = df['col1'].isna()
s = df.loc[filters, 'col2'].copy()
df = df[~filters]
df['col3'] = s.values
Edit: as you mentioned, the filter you want is 'None'
, not None
, then:
filters = df['col1'].eq('None')
Maybe you need to check some edge situation , I thought the ID is the key to assign , if you only assign it by value , ID may mismatch
– WeNYoBen
6 hours ago
Agree with @WeNYoBen, its dangerous just topaste
the values as a column
– Erfan
6 hours ago
That's true. But theID
column given is not unique-value, so assign on that would fail (I think). Or I need to do a groupby.
– Quang Hoang
6 hours ago
add a comment |
Try:
filters = df['col1'].isna()
s = df.loc[filters, 'col2'].copy()
df = df[~filters]
df['col3'] = s.values
Edit: as you mentioned, the filter you want is 'None'
, not None
, then:
filters = df['col1'].eq('None')
Maybe you need to check some edge situation , I thought the ID is the key to assign , if you only assign it by value , ID may mismatch
– WeNYoBen
6 hours ago
Agree with @WeNYoBen, its dangerous just topaste
the values as a column
– Erfan
6 hours ago
That's true. But theID
column given is not unique-value, so assign on that would fail (I think). Or I need to do a groupby.
– Quang Hoang
6 hours ago
add a comment |
Try:
filters = df['col1'].isna()
s = df.loc[filters, 'col2'].copy()
df = df[~filters]
df['col3'] = s.values
Edit: as you mentioned, the filter you want is 'None'
, not None
, then:
filters = df['col1'].eq('None')
Try:
filters = df['col1'].isna()
s = df.loc[filters, 'col2'].copy()
df = df[~filters]
df['col3'] = s.values
Edit: as you mentioned, the filter you want is 'None'
, not None
, then:
filters = df['col1'].eq('None')
answered 6 hours ago
Quang HoangQuang Hoang
5,96411021
5,96411021
Maybe you need to check some edge situation , I thought the ID is the key to assign , if you only assign it by value , ID may mismatch
– WeNYoBen
6 hours ago
Agree with @WeNYoBen, its dangerous just topaste
the values as a column
– Erfan
6 hours ago
That's true. But theID
column given is not unique-value, so assign on that would fail (I think). Or I need to do a groupby.
– Quang Hoang
6 hours ago
add a comment |
Maybe you need to check some edge situation , I thought the ID is the key to assign , if you only assign it by value , ID may mismatch
– WeNYoBen
6 hours ago
Agree with @WeNYoBen, its dangerous just topaste
the values as a column
– Erfan
6 hours ago
That's true. But theID
column given is not unique-value, so assign on that would fail (I think). Or I need to do a groupby.
– Quang Hoang
6 hours ago
Maybe you need to check some edge situation , I thought the ID is the key to assign , if you only assign it by value , ID may mismatch
– WeNYoBen
6 hours ago
Maybe you need to check some edge situation , I thought the ID is the key to assign , if you only assign it by value , ID may mismatch
– WeNYoBen
6 hours ago
Agree with @WeNYoBen, its dangerous just to
paste
the values as a column– Erfan
6 hours ago
Agree with @WeNYoBen, its dangerous just to
paste
the values as a column– Erfan
6 hours ago
That's true. But the
ID
column given is not unique-value, so assign on that would fail (I think). Or I need to do a groupby.– Quang Hoang
6 hours ago
That's true. But the
ID
column given is not unique-value, so assign on that would fail (I think). Or I need to do a groupby.– Quang Hoang
6 hours ago
add a comment |
Yet another attempt:
f=df['col1']=='None'
c3=df.loc[f].col2.reset_index(drop=True)
df=df[~f]
df2=pd.concat([df.reset_index(drop=True),c3], axis=1, ignore_index=True)
df2.columns=['ID', 'col1', 'col2', 'col3']
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
add a comment |
Yet another attempt:
f=df['col1']=='None'
c3=df.loc[f].col2.reset_index(drop=True)
df=df[~f]
df2=pd.concat([df.reset_index(drop=True),c3], axis=1, ignore_index=True)
df2.columns=['ID', 'col1', 'col2', 'col3']
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
add a comment |
Yet another attempt:
f=df['col1']=='None'
c3=df.loc[f].col2.reset_index(drop=True)
df=df[~f]
df2=pd.concat([df.reset_index(drop=True),c3], axis=1, ignore_index=True)
df2.columns=['ID', 'col1', 'col2', 'col3']
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
Yet another attempt:
f=df['col1']=='None'
c3=df.loc[f].col2.reset_index(drop=True)
df=df[~f]
df2=pd.concat([df.reset_index(drop=True),c3], axis=1, ignore_index=True)
df2.columns=['ID', 'col1', 'col2', 'col3']
ID col1 col2 col3
0 1 Abc street 2017-07-27 2017-08-17
1 1 Def street 2018-07-15 2018-08-13
2 2 fbg street 2018-01-07 2018-08-12
3 2 trf street 2019-01-15 NaN
answered 5 hours ago
prostiprosti
6,86913241
6,86913241
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.
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%2f56117560%2ffilter-a-data-frame-and-add-a-new-column-according-to-the-given-condition%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
Is it
None
or'None'
?– user3483203
6 hours ago
oh!!. it's actually 'None'
– No_body
6 hours ago