Could pandas use column as index?
Yes, with pandas.DataFrame.set_index
you can make 'Locality'
your row index.
data.set_index('Locality', inplace=True)
If inplace=True
is not provided, set_index
returns the modified dataframe as a result.
Example:
> import pandas as pd
> df = pd.DataFrame([['ABBOTSFORD', 427000, 448000],
['ABERFELDIE', 534000, 600000]],
columns=['Locality', 2005, 2006])
> df
Locality 2005 2006
0 ABBOTSFORD 427000 448000
1 ABERFELDIE 534000 600000
> df.set_index('Locality', inplace=True)
> df
2005 2006
Locality
ABBOTSFORD 427000 448000
ABERFELDIE 534000 600000
> df.loc['ABBOTSFORD']
2005 427000
2006 448000
Name: ABBOTSFORD, dtype: int64
> df.loc['ABBOTSFORD'][2005]
427000
> df.loc['ABBOTSFORD'].values
array([427000, 448000])
> df.loc['ABBOTSFORD'].tolist()
[427000, 448000]
Pandas index column title or name
You can just get/set the index via its name
property
In [7]: df.index.name
Out[7]: 'Index Title'
In [8]: df.index.name = 'foo'
In [9]: df.index.name
Out[9]: 'foo'
In [10]: df
Out[10]:
Column 1
foo
Apples 1
Oranges 2
Puppies 3
Ducks 4
In pandas, how to set_index with using column index instead of referring to column names?
If the column index is unique you could use:
df.set_index(list(df.columns[cols]))
where cols
is a list of ordinal indices.
For example,
In [77]: np.random.seed(2016)
In [79]: df = pd.DataFrame(np.random.randint(10, size=(5,4)), columns=list('ABCD'))
In [80]: df
Out[80]:
A B C D
0 3 7 2 3
1 8 4 8 7
2 9 2 6 3
3 4 1 9 1
4 2 2 8 9
In [81]: df.set_index(list(df.columns[[0,2]]))
Out[81]:
B D
A C
3 2 7 3
8 8 4 7
9 6 2 3
4 9 1 1
2 8 2 9
If the DataFrame's column index is not unique, then setting the index by label
is impossible and by ordinals more complicated:
import numpy as np
import pandas as pd
np.random.seed(2016)
def set_ordinal_index(df, cols):
columns, df.columns = df.columns, np.arange(len(df.columns))
mask = df.columns.isin(cols)
df = df.set_index(cols)
df.columns = columns[~mask]
df.index.names = columns[mask]
return df
df = pd.DataFrame(np.random.randint(10, size=(5,4)), columns=list('AAAA'))
print(set_ordinal_index(df, [0,2]))
yields
A A
A A
3 2 7 3
8 8 4 7
9 6 2 3
4 9 1 1
2 8 2 9
Get column index from column name in python pandas
Sure, you can use .get_loc()
:
In [45]: df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})
In [46]: df.columns
Out[46]: Index([apple, orange, pear], dtype=object)
In [47]: df.columns.get_loc("pear")
Out[47]: 2
although to be honest I don't often need this myself. Usually access by name does what I want it to (df["pear"]
, df[["apple", "orange"]]
, or maybe df.columns.isin(["orange", "pear"])
), although I can definitely see cases where you'd want the index number.
python - How to set column as index in a DataFrame
I think you need transpose
by T
and then if necessary change column names add list comprehension:
Notice:
Double groupby+sum
is not necessary, once is enough, because aggregate same aggregate function, here sum
.
df = pd.DataFrame(salary_List)
newdf = df.groupby(pd.Grouper(key='date', freq='1M')).sum().T
#python 3.6+
newdf.columns = [f'Month{x}' for x in range(1, len(newdf.columns) + 1)]
#python bellow
#newdf.columns = ['Month{}'.format(x) for x in range(1, len(newdf.columns) + 1)]
print (newdf)
Month1 Month2 Month3 Month4 Month5 \
Balance before Salary 27.2 88.2 176.48 48.48 241.48
Salary 15300.0 15300.0 14783.00 16249.00 14448.00
Month6
Balance before Salary 49.48
Salary 15663.00
Pandas (python): How to add column to dataframe for index?
How about this:
from pandas import *
idx = Int64Index([171, 174, 173])
df = DataFrame(index = idx, data =([1,2,3]))
print df
It gives me:
0
171 1
174 2
173 3
Is this what you are looking for?
Related Topics
Splitting a Pandas Dataframe Column by Delimiter
How to Escape Strings for SQLite Table/Column Names in Python
Collision Between Masks in Pygame
How to Profile Python Code Line-By-Line
Spark Dataframe Distinguish Columns with Duplicated Name
Can Python Pickle Lambda Functions
Ssl.Sslerror: Tlsv1 Alert Protocol Version
Python JSON Parser Allow Duplicate Keys
Plotting a Decision Boundary Separating 2 Classes Using Matplotlib's Pyplot
Matplotlib Xticks Not Lining Up with Histogram
Flask to Return Image Stored in Database
How to Recursively Find Specific Key in Nested JSON
Pygame.Error: Video System Not Initialized
Split a Python List into Other "Sublists" I.E Smaller Lists
Find Out How Much Memory Is Being Used by an Object in Python
Split a Generator into Chunks Without Pre-Walking It