How to write data from pandas.dataframe to a file

Import python packages commonly used in experiments. as shown in picture 2.

[import pandas as pd] pandas is used for data processing. [import numpy as np] numpy is used for high-dimensional matrix operations. [import matplotlib.pyplot as plt] matplotlib is used for data visualization.

3. Pandas data is written to the csv file:

[names = ['Bob','Jessica','Mary','John','Mel']]Create a names list [births = [968,155,77,578,973]]Create a births list [DataSet = list(zip(names ,births))] Use the zip function to merge the two lists together [DataSet] View the generated data [df = pd.DataFrame(data = DataSet ,columns=['Names','Births'])] Use the generated data The data generates a DataFrame object [df] to view the generated dataFrame

Write the created data to the /opt/births1880.csv file,

[df.to_csv('/opt/births1880.csv', index=False, header=False )] Write df to the file [ls /opt/births1880.csv] Check if the file exists [cat /opt/births1880. csv] View file content

4. Pandas reads data in csv

Read the data generated in step 3, as shown in Figure 5. [local_data = r'/opt/births1880.csv'] Assign the file path to the variable local_data [df2 = pd.read_csv(local_data, header=None)] Read the content and assign it to df2 [df2] View the value of df2 [df3 = pd.read_csv(local_data,header=None,names=['names','births'])] Assign the column name to df3[df3] View the value of df3

Pandas is the next open source data analysis library in Python. The data structure DataFrame it provides greatly simplifies some tedious operations in the data analysis process. DataFrame is a multi-dimensional table. You can think of it as an Excel form or Sql table. . The previous article has covered loading raw data into dataframes from various data sources. This document describes how to write processed dataframes to files and databases.

Cao Cao Zhiyong Guangsheng Shanjiao

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325730648&siteId=291194637