if you just want to overwrite the index values then do:
In[32]:
test3.index = ['f', 'g', 'z']
test3
Out[32]:
f 1
g 2
z 3
dtype: int64
Create a new index and reindex the dataframe. By default values in the new index that do not have corresponding records in the dataframe are assigned NaN.,The index entries that did not have a value in the original data frame (for example, ‘2009-12-29’) are by default filled with NaN. If desired, we can fill in the missing values using one of several options.,To further illustrate the filling functionality in reindex, we will create a dataframe with a monotonically increasing index (for example, a sequence of dates).,Conform DataFrame to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False
>>> index = ['Firefox', 'Chrome', 'Safari', 'IE10', 'Konqueror'] >>>
df = pd.DataFrame({
...'http_status': [200, 200, 404, 404, 301],
...'response_time': [0.04, 0.02, 0.07, 0.08, 1.0]
},
...index = index) >>>
df
http_status response_time
Firefox 200 0.04
Chrome 200 0.02
Safari 404 0.07
IE10 404 0.08
Konqueror 301 1.00
>>> new_index = ['Safari', 'Iceweasel', 'Comodo Dragon', 'IE10',
...'Chrome'
] >>>
df.reindex(new_index)
http_status response_time
Safari 404.0 0.07
Iceweasel NaN NaN
Comodo Dragon NaN NaN
IE10 404.0 0.08
Chrome 200.0 0.02
>>> df.reindex(new_index, fill_value = 0) http_status response_time Safari 404 0.07 Iceweasel 0 0.00 Comodo Dragon 0 0.00 IE10 404 0.08 Chrome 200 0.02
>>> df.reindex(new_index, fill_value = 'missing')
http_status response_time
Safari 404 0.07
Iceweasel missing missing
Comodo Dragon missing missing
IE10 404 0.08
Chrome 200 0.02
>>> df.reindex(columns = ['http_status', 'user_agent'])
http_status user_agent
Firefox 200 NaN
Chrome 200 NaN
Safari 404 NaN
IE10 404 NaN
Konqueror 301 NaN
>>> df.reindex(['http_status', 'user_agent'], axis = "columns")
http_status user_agent
Firefox 200 NaN
Chrome 200 NaN
Safari 404 NaN
IE10 404 NaN
Konqueror 301 NaN
I have a dataframe of the following:, 3 days ago Sep 22, 2019 · Pandas reindex converts all values to NaN. Ask Question Asked 2 years, 11 months ago. Modified 2 years, 11 months ago. ... import random import datetime import pandas as pd a = pd.DataFrame({'values':[random.randint(-10,10) for i in range(10)]}) a['times'] = [datetime.datetime(2018,1,2,12,40,0) + datetime.timedelta(seconds=i) for i in range(10 ... , 1 week ago Jul 24, 2021 · You can then create a DataFrame in Python to capture that data:. import pandas as pd import numpy as np df = pd.DataFrame({'values': [700, np.nan, 500, np.nan]}) print (df) Run the code in Python, and you’ll get the following DataFrame with the NaN values:. values 0 700.0 1 NaN 2 500.0 3 NaN . In order to replace the NaN values with zeros for a column using … , Reindexing in Pandas can be used to change the index of rows and columns of a DataFrame. Indexes can be used with reference to many index DataStructure associated with several pandas series or pandas DataFrame.
>>> a = pd.DataFrame({
'values': [random.randint(-10, 10) for i in range(10)]
}) >>> a values 0 - 3 1 - 8 2 - 2 3 3 4 8 5 6 6 - 5 7 0 8 8 9 - 4
df = df.set_index([times]) Out[64]: values 2018 - 01 - 02 12: 40: 00 - 3 2018 - 01 - 02 12: 40: 01 - 8 2018 - 01 - 02 12: 40: 02 - 2 2018 - 01 - 02 12: 40: 03 3 2018 - 01 - 02 12: 40: 04 8 2018 - 01 - 02 12: 40: 05 6 2018 - 01 - 02 12: 40: 06 - 5 2018 - 01 - 02 12: 40: 07 0 2018 - 01 - 02 12: 40: 08 8 2018 - 01 - 02 12: 40: 09 - 4
Pandas reindex converts all values to NaN,Pandas converting all data to NaN after adding column values,Python Pandas find all rows where all values are NaN,combine row values in all consecutive rows that contains NaN and int values using pandas
as long as you have size of times
the same as df.size
, you may pass it to set_index
df = df.set_index([times]) Out[64]: values 2018 - 01 - 02 12: 40: 00 - 3 2018 - 01 - 02 12: 40: 01 - 8 2018 - 01 - 02 12: 40: 02 - 2 2018 - 01 - 02 12: 40: 03 3 2018 - 01 - 02 12: 40: 04 8 2018 - 01 - 02 12: 40: 05 6 2018 - 01 - 02 12: 40: 06 - 5 2018 - 01 - 02 12: 40: 07 0 2018 - 01 - 02 12: 40: 08 8 2018 - 01 - 02 12: 40: 09 - 4
Or you assign it directly to index
In[67]: df.index = times
In[68]: df
Out[68]:
values
2018 - 01 - 02 12: 40: 00 - 3
2018 - 01 - 02 12: 40: 01 - 8
2018 - 01 - 02 12: 40: 02 - 2
2018 - 01 - 02 12: 40: 03 3
2018 - 01 - 02 12: 40: 04 8
2018 - 01 - 02 12: 40: 05 6
2018 - 01 - 02 12: 40: 06 - 5
2018 - 01 - 02 12: 40: 07 0
2018 - 01 - 02 12: 40: 08 8
2018 - 01 - 02 12: 40: 09 - 4
Code
import random
import datetime
import pandas as pd
a = pd.DataFrame({
'values': [random.randint(-10, 10) for i in range(10)]
})
a['times'] = [datetime.datetime(2018, 1, 2, 12, 40, 0) + datetime.timedelta(seconds = i) for i in range(10)]
a = a.set_index('times')
Result
times values 2018 - 01 - 02 12: 40: 00 - 2 2018 - 01 - 02 12: 40: 01 - 3 2018 - 01 - 02 12: 40: 02 5 2018 - 01 - 02 12: 40: 03 - 9 2018 - 01 - 02 12: 40: 04 - 6 2018 - 01 - 02 12: 40: 05 2 2018 - 01 - 02 12: 40: 06 1 2018 - 01 - 02 12: 40: 07 - 1 2018 - 01 - 02 12: 40: 08 5 2018 - 01 - 02 12: 40: 09 3
We can fill the null values using the parameter fill_value=2 in the DataFrame.reindex() method. After changing the index name, if there is a null value, that null values will be filled by the value 2.,Here, we reindex the index of the DataFrame using the DataFrame.reindex() method. The index which is not in the original DataFrame will automatically be filled by the NaN values. See the below example.,We can also reindex the column of the DataFrame using the DataFrame.reindex() method. The column which is not in the original DataFrame will automatically be filled by the NaN values. See the below example.,In this tutorial, we will discuss and learn the Python pandas DataFrame.reindex() method. By using this method, we can change the name of the index and columns. This method reconciles the DataFrame to the new index with optional filling logic. It places null values in locations having no value in the previous index.
Syntax
DataFrame.reindex(labels = None, index = None, columns = None, axis = None, method = None, copy = True, level = None, fill_value = nan, limit = None, tolerance = None)
Here, we reindex the index of the DataFrame using the DataFrame.reindex()
method. The index which is not in the original DataFrame will automatically be filled by the NaN values. See the below example.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df = pd.DataFrame([
[1, 6, 2],
[3, 4, 6],
[12, 1, 0]
], columns = ['A', 'B', 'C'], index = (['index_1', 'index_2', 'index_3']))
print("--------The DataFrame is----------")
print(df)
print("---------------------------------")
index = ['index_1', 'index_2', 'index_4']
print(df.reindex(index))
We can also reindex the column of the DataFrame using the DataFrame.reindex()
method. The column which is not in the original DataFrame will automatically be filled by the NaN values. See the below example.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df = pd.DataFrame([
[1, 6, 2],
[3, 4, 6],
[12, 1, 0]
], columns = ['A', 'B', 'C'], index = (['index_1', 'index_2', 'index_3']))
print("--------The DataFrame is----------")
print(df)
print("---------------------------------")
column = ['A', 'C', 'D']
print(df.reindex(column, axis = "columns"))