This question already has an answer here:
Python Pandas Key Error When Trying to Access Index
(1 answer)
Closed 2 years ago.
My DataFrame looks like this :
# Pivoting the DF
df = df.pivot(index='Date',columns='Track Name',values='Streams')
After pivoting, I cant access the Index key.
The DataFrame looks like this :
Can any one tell me what am I doing wrong ? I am new with Pandas, reference to helpful recourses is welcome.
You can try this:
df.index.values
It will return a numpy array of the values of your index column
and you can then slice it like that df.index.values[0].
Or if it is more preferable you convert it to a list:
list(df.index.values)
Related
This question already has answers here:
How to convert index of a pandas dataframe into a column
(9 answers)
Closed last month.
I'm reading the data I'm working on and making it organized with the following codes.
import pandas as pd
df = pd.read_csv("data.csv").assign(date=lambda x: pd.to_datetime(x['date']))
.groupby([pd.Grouper(key='date', freq='M'), pd.Grouper(key='item_id')]).count().reset_index()
.pivot('date', 'item_id').fillna(0).astype(int)
This way I can see the indexes and their values.
What should I do if I want to operate using the values in the indexes? How can I access them?
You can treat your index as a normal column:
df['new_column'] = df.index
Or
df = df.reset_index(drop=False)
This question already has answers here:
How to deal with SettingWithCopyWarning in Pandas
(20 answers)
Closed 1 year ago.
This question is probably very simple, but I seem to be having trouble creating a new column in a dataframe and filling that column with a numpy array. I have an array i.e. [0,0,0,1,0,1,1] and a dataframe that has the same number of rows as the length of that array. I want to add a column and I have been doing this:
df['new_col'] = array
however I get the following warning error:
A value is trying to be set on a copy of a slice from a DataFrame. Try
using .loc[row_indexer,col_indexer] = value instead
I tried to do df.loc[:,'new_col'] = array but get the same warning error. I also tried:
df.loc['new_col'] = pd.Series(array, index = df.index)
based on a different answer from a question a different user asked. Does anyone know a "better" way to code this? Or should I just ignore the warning messages?
Code from https://www.geeksforgeeks.org/adding-new-column-to-existing-dataframe-in-pandas/
Import pandas package
import pandas as pd
Define a dictionary containing data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Height': [5.1, 6.2, 5.1, 5.2]
}
Convert the dictionary into DataFrame
original_df = pd.DataFrame(data)
Using 'Qualification' as the column name and equating it to the list
altered_df = original_df.assign(Qualification = ['Msc', 'MA', 'Msc', 'Msc'])
Observe the result
altered_df
The DataFrame expects a list input (Each column is like a dictionary with columns as keys and a list as values)
Try this using the tolist() method on the numpy array:
df['new_col'] = array.tolist()
This question already has answers here:
How to pivot a dataframe in Pandas? [duplicate]
(2 answers)
Closed 1 year ago.
Hi there I have a data set look like df1 below and I want to make it look like df2 using pandas. I have tried to use pivot and transpose but can't wrap my head around how to do it. Appreciate any help!
This should do the job
df.pivot_table(index=["AssetID"], columns='MeterName', values='MeterValue')
index: Identifier
columns: row values that will become columns
values: values to put in those columns
I often have the same trouble:
https://towardsdatascience.com/reshape-pandas-dataframe-with-pivot-table-in-python-tutorial-and-visualization-2248c2012a31
This could help next time.
This question already has answers here:
How to unnest (explode) a column in a pandas DataFrame, into multiple rows
(16 answers)
Closed 1 year ago.
I have dataframe like this
Is there any way to convert to this
I tried to traverse data frame than traverse grades array and add add to new data frame but it doesn't seem most efficient or easy way is there any built in method or better way to approach this problem
PS: I searched for similar questions but I couldn't find it if there is question like this I am very sorry ı will delete immediately
What you want is pandas.DataFrame.explode().
import ast
# Make sure column A is list first.
df['A'] = df['A'].apply(ast.literal_eval)
# or
df['A'] = df['A'].apply(pd.eval)
df = df.explode('Grades')
df = df.rename(columns={'Grades': 'Grade'})
This question already has answers here:
How can I pivot a dataframe?
(5 answers)
Closed 4 years ago.
I have a dataframe which is similar to:
grades=pd.DataFrame(columns=["person","course_code","grade"],data=[[1,101,2.0],[2,102,1.0],[3,103,3.0],[2,104,4.0],[1,102,5.0],[3,104,2.5],[2,101,1.0]])
On each row is the grade of a certain student in certain subject.
And want to convert it to another that looks like this:
students=pd.DataFrame(columns=[101,102,103,104],data [[2.0,5.0,"NaN","NaN"],[1.0,1.0,"Nan",4.0],["Nan","Nan",3.0,2.5]])
On each row is a student (codex of the row) with the different grades obtained in every subject (every column is a different subject).
I have tried doing this:
for subj in grades["COURSE_CODE"].unique():
grades_subj=grades[grades["COURSE_CODE"]==subj]
grades_subj = grades_subj.set_index("EXPEDIENT_CODE", drop = True)
for st in grades["EXPEDIENT_CODE"].unique():
grade_num=grades_subj.loc[st]["GRADE"]
student.loc[st][subj]=grade_num
But I get:
KeyError: 'the label [304208] is not in the [index]'
I have tried other ways too and get always errors...
Can someone help me, please?
try:
grades.pivot_table(index='person', columns='course_code', values='grade')
The value argument let you to choose the aggregation column.
In order to answer your comment below, you can always add different levels when indexing. This is simply done by passing a list rather than a single string to index. Note you can do the same in columns. SO, based in the example you provide.
grades.pivot_table(index=['person','school'], columns='course_code', values ='grade')
After this I usually recommend to reset_index() unless you are fluent slicing and indexing with MultiIndex.
Also, if the correspondence is 1 to 1, you could merge both dataframes using the appropiate join.
Here you have all the information about Reshaping and Pivot Tables in Pandas.