String manipulation with python & pandas that I often use

Read CSV file

python


#Open file
path=""
filename="hoge"
df = pd.read_csv('{}/{}.csv'.format(path,filename))

This is recommended because it is simpler and easier to understand. I asked @shiracamus to teach me.

python


#Open file
path=""
filename="hoge"
df = pd.read_csv(f'{path}/{filename}.csv')

Save as CSV file (no index)

python


#Save file
path=""
filename="hoge"
df.to_csv('{}/{}.csv'.format(path,filename), index=False)

Similarly, this is recommended because it is simpler and easier to understand. I asked @shiracamus to teach me.

python


#Save file
path=""
filename="hoge"
df.to_csv(f'{path}/{filename}.csv', index=False)

Get the number of rows and columns

python


line = df.shape[0] #line
row = df.shape[1] #Column

Get the data in i rows and j columns

python


hoge = df.iat[i, j]

Make the list df (with columns)

python


list=[hoge1,hoge2,hoge3]
df = pd.DataFrame(list, columns=["hoge"])

Concatenate df

python


df = pd.concat([df1, df2], axis=1) #Vertical:0 sideways:1

Get the very first data in column j from df

python


hoge = df.iat[0, j]

Get the last data in column j from df

python


line = df.shape[0] #line
hoge = df.iat[line, j]

index setting

python


df = df.set_index('Column name')

Release index

index returns to 1,2,3 ..., and the column specified in index returns to its original position?

python


df = df.reset_index('Column name')

Change from str type to datetime type

If you specify a new column name, the column will be added, and if it is an existing column name, it will be overwritten.

python


df["Column name"] = pd.to_datetime(df["Column name"])

Change from datetime type to str type

If you specify a new column name, the column will be added, and if it is an existing column name, it will be overwritten.

python


df["Column name"] = df["Column name"].to_string()

Recommended Posts

String manipulation with python & pandas that I often use
I want to use a wildcard that I want to shell with Python remove
[Python] Use string data with scikit-learn SVM
Features of regular expression modules that I often use personally in Python
I want to use MATLAB feval with python
I want to use Temporary Directory with Python2
[Python] Use JSON with Python
String manipulation in python
Python string manipulation master
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Data manipulation with Pandas!
Use Cursur that closes automatically with sqlite3 in Python
I tried to summarize how to use pandas in python
A memo that I touched the Datastore with python
A template that I often use when making Discord BOT in Python (memorial note)
I tried fp-growth with python
I tried scraping with Python
Use Trello API with python
[Python] Use a string sequence
I made blackjack with python!
Use TUN / TAP with Python
[Python] Change dtype with pandas
Regular expression manipulation with Python
String date manipulation in Python
I tried gRPC with Python
Use subsonic API with python3
I tried scraping with python
I made blackjack with Python.
String format with Python% operator
I made wordcloud with Python.
Why can I use the module by importing with python?
python Condition extraction from a list that I often forget
Use a macro that runs when saving python with vscode
[Python] I want to use the -h option with argparse
I couldn't use tkinter with python installed by pyenv of anyenv
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
I made a package that can compare morphological analyzers with Python
[Python] How to use Pandas Series
I can't install python3 with pyenv-vertualenv
When I tried to use pip with python, I was told that XML_SetHashSalt could not be found.
I tried web scraping with python.
[Python] A memo that I tried to get started with asyncio
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
I made a fortune with Python.
I sent an SMS with Python
Use rospy with virtualenv in Python3
[Python] Join two tables with pandas
String replacement with Python regular expression
[Pandas] I tried to analyze sales data with Python [For beginners]
[Introduction to Python] Let's use pandas
I made a shuffle that can be reset (reverted) with Python
Use Python in pyenv with NeoVim
I liked the tweet with python. ..
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
How to use FTP with Python