cómo cambiar indeces en pandas dataframe
>>> df.set_index('month')
year sale
month
1 2012 55
4 2014 40
7 2013 84
10 2014 31
pandas de reset_index
df.reset_index(drop=True, inplace=True)
establecer el índice de la columna de los pandas
# method A
df = df.set_index('col')
# method B
df['col'] = df.index
cómo cambiar el índice en dataframe python
index = [1,2]
df.index = index
los pandas crear una columna de índice
df.reset_index(level=0, inplace=True)
cómo establecer el índice de pandas
# assignment copy
df = df.set_index('month')
# or inplace
df.set_index('month', inplace=True)
# year sale month month year sale
# 0 2012 55 1 1 2012 55
# 1 2014 40 4 => 4 2014 40
# 2 2013 84 7 7 2013 84
# 3 2014 31 10 10 2014 31
establecer índice en datarame
df = df.set_index('col')
cómo configurar pandas índice
# assignment copy
df = df.set_index('month')
# or inplace
df.set_index('month', inplace=True)
# year sale month month year sale
# 0 2012 55 1 1 2012 55
# 1 2014 40 4 => 4 2014 40
# 2 2013 84 7 7 2013 84
# 3 2014 31 10 10 2014 31
agregar una columna de índice pandas
import numpy as np
import pandas as pd
df = pd.DataFrame({'month': [2, 5, 8, 10],
'year': [2017, 2019, 2018, 2019],
'sale': [60, 45, 90, 36]})
df.set_index('month')
los pandas df convierten el conjunto de columna de índice
df.reset_index(inplace=True)
df = df.rename(columns = {'index':'new column name'})
cómo configurar pandas índice
# assignment copy
df = df.set_index('month')
# or inplace
df.set_index('month', inplace=True)
# year sale month month year sale
# 0 2012 55 1 1 2012 55
# 1 2014 40 4 => 4 2014 40
# 2 2013 84 7 7 2013 84
# 3 2014 31 10 10 2014 31
str reemplazar pandas
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f.', 'ba', regex=True)
0 bao
1 baz
2 NaN
dtype: object