DataFrame of pandas From creating a DataFrame from two lists to writing a file

at first

DataFrame is difficult. I'm worried about the next problem.

--Store list1 and list2 in index 0 and index 1? --A pair where the indexes of list1 and list2 are aligned?

My idea is "I want to know Alice 's height ". This says, "Oh, you should pair Alice and Alice's height!"

In the following, write down from the two arrays to writing the file via DataFrame.

Two 1D arrays → 2D arrays → DataFrame → csv file

import pandas as pd

list1 = ['Alice', 'Bob', 'Charlie']
list2 = ['165', '175', '180']

dim2list = [[list1[i], list2[i]] for i in range(len(list1))]
df = pd.DataFrame(dim2list, columns=['Name', 'Length'])

df.to_csv('output.csv', index=False, header=True)

output.csv


Name,Length
Alice,165
Bob,175
Charlie,180

Two 1D arrays → 2D arrays → DataFrame → txt files

import pandas as pd

list1 = ['Alice', 'Bob', 'Charlie']
list2 = ['165', '175', '180']

dim2list = [[list1[i], list2[i]] for i in range(len(list1))]
df = pd.DataFrame(dim2list, columns=['Name', 'Length'])

df.to_csv('output.txt', sep='\t', index=False, header=True)

output.txt


Name	Length
Alice	165
Bob	175
Charlie	180

Reference article

Recommended Posts

DataFrame of pandas From creating a DataFrame from two lists to writing a file
Put the lists together in pandas to make a DataFrame
Create a pandas Dataframe from a string.
[Pandas_flavor] Add a method of Pandas DataFrame
Create a dataframe from excel using pandas
Download Pandas DataFrame as a CSV file
[Python] How to read a csv file (read_csv method of pandas module)
[python] Create table from pandas DataFrame to postgres
A story of creating 16 * 16 dots from a Digimon photo
Make a copy of a Google Drive file from Python
Steps from installing Python 3 to creating a Django app
Python script to create a JSON file from a CSV file
Created a tool to output a sequence diagram from a packet capture file of multiple nodes
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
I want to start a lot of processes from python
A handy function to add a column anywhere in a Pandas DataFrame
[Python] How to output a pandas table to an excel file
A quick explanation from creating AWS Lambda Layers to linking
Upload a file to Dropbox
Formatted display of pandas DataFrame
Export pandas dataframe to excel
How to paste a CSV file into an Excel file using Pandas
I tried to get a database of horse racing using Pandas
Conditional branch due to the existence of a shell script file
[Python] How to add rows and columns to a table (pandas DataFrame)
TensorFlow To learn from a large number of images ... ~ (almost) solution ~
How to output the output result of the Linux man command to a file
The story of creating a site that lists the release dates of books
Output search results of posts to a file using Mattermost API
Python vba to create a date string for creating a file name
I made a subtitle file (SRT) from JSON data of AmiVoice
Get OCTA simulation conditions from a file and save with pandas
Let Code Day 35 "160. Intersection of Two Linked Lists" Starting from Zero
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
[Python] Operation memo of pandas DataFrame
Etosetra related to read_csv of Pandas
Write standard output to a file
In python pandas SettingWithCopyWarning 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
How to create a config file
The story of writing a program
How to get a specific column name and index name in pandas DataFrame
[Python / Tkinter] Search for Pandas DataFrame → Create a simple search form to display
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
A memorandum of how to write pandas that I tend to forget personally
[Python memo] Be careful when creating a two-dimensional array (list of lists)
How to put a line number at the beginning of a CSV file
I made a program to check the size of a file in Python
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved
Use BeautifulSoup to extract a link containing a string from an HTML file
Let Code Day6 Starting from Zero "1342. Number of Steps to Reduce a Number to Zero"