Skip to content

Instantly share code, notes, and snippets.

@beomjunshin-ben
Last active July 28, 2020 01:42
Show Gist options
  • Save beomjunshin-ben/2d2d02a61da23ff8ef5d to your computer and use it in GitHub Desktop.
Save beomjunshin-ben/2d2d02a61da23ff8ef5d to your computer and use it in GitHub Desktop.
df = pd.DataFrame({'a': [5,1,6,2,23], 'b': [7,7,7,7,7]})
"""
Out:
a b
0 5 7
1 1 7
2 6 7
3 2 7
4 23 7
"""
df.loc[(df.a < 5), ['a']] = pd.Series([100,100]) # WAT
"""
Out:
a b
0 5 7
1 100 7
2 6 7
3 NaN 7
4 23 7
"""
df.loc[(df.a < 5), 'a'] = [100, 100] # Good
"""
Out[103]:
a b
0 5 7
1 100 7
2 6 7
3 100 7
4 23 7
"""
df.loc[(df.a < 5), ['a', 'b']] = [[100, 100], [200, 300]] # Good
"""
Out[111]:
a b
0 5 7
1 100 100
2 6 7
3 200 300
4 23 7
"""
series = df[df.a < 5].apply(lambda row: pd.Series([row['a']+100, row['b']+200]), axis=1)
"""
0 1
1 101 207
3 102 207
"""
noseries = df[df.a < 5].apply(lambda row: [row['a']+100, row['b']+200], axis=1)
"""
a b
1 101 207
3 102 207
"""
df[df.a < 5] = series # Done
"""
a b
0 5 7
1 101 207
2 6 7
3 102 207
4 23 7
"""
df[df.a < 5] = noseries # Done
"""
a b
0 5 7
1 101 207
2 6 7
3 102 207
4 23 7
"""
series_one = df[df.a < 5].apply(lambda row: pd.Series([row['a']+100]), axis=1) # only one column
"""
0
1 101
3 102
"""
noseries_one = df[df.a < 5].apply(lambda row: [row['a']+100], axis=1)
"""
a b
1 101 101
3 102 102
"""
noseries = df[df.a < 5].apply(lambda row: row['a']+100, axis=1)
"""
1 101
3 102
"""
@beomjunshin-ben
Copy link
Author

df.loc[(df.a < 5), ['a', 'b']] = [[100, 100], [200, 300]] # Good 로 결국 해결

@beomjunshin-ben
Copy link
Author

Series 는 Index로 매칭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment