환경
- Python
- Pandas
사용법
.astype()
사용
import pandas as pd
df = pd.DataFrame({'A': ['1', '2', '3'], 'B': [4, 5, 6]})
print(df.dtypes)
# convert column 'A' from string to int
df['A'] = df['A'].astype(int)
print(df.dtypes)
.astype()
사용import pandas as pd
df = pd.DataFrame({'A': ['1', '2', '3'], 'B': [4, 5, 6]})
print(df.dtypes)
# convert column 'A' from string to int
df['A'] = df['A'].astype(int)
print(df.dtypes)
.astype()
import pandas as pd
df = pd.DataFrame({'A': ['1', '2', '3'], 'B': [4, 5, 6]})
print(df.dtypes)
# convert column 'A' from string to int
df['A'] = df['A'].astype(int)
print(df.dtypes)
pd.concat()
사용import pandas as pd
# create an existing DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# create a new row as a DataFrame
new_row = pd.DataFrame({'A': [4], 'B': [7]})
# concatenate the existing DataFrame and the new row
df = pd.concat([df, new_row], ignore_index=True)
df
pd.concat()
import pandas as pd
# create an existing DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# create a new row as a DataFrame
new_row = pd.DataFrame({'A': [4], 'B': [7]})
# concatenate the existing DataFrame and the new row
df = pd.concat([df, new_row], ignore_index=True)
df
df.loc[]
사용import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]})
# modify values in column 'B' and 'C' based on a condition in column 'A'
df.loc[df['A'] < 3, ['B', 'C']] = 0
df
np.where()
사용import pandas as pd
import numpy as np
df = pd.DataFrame({'A': range(0, 30), 'B': range(30, 60)})
# make condition using & or |
cond_1 = (5 <= df['A']) & (df['A'] <= 10)
cond_2 = (15 <= df['A']) & (df['A'] <= 20)
print(np.where( cond_1 | cond_2))
# create a new column C based on values in column A and an if condition
df['A is in 5~10 or 15~20'] = np.where(cond_1 | cond_2, True, False)
df